使用LINQ to XML解析嵌套数组

时间:2014-03-19 10:14:47

标签: c# xml linq-to-xml

我有xml如下所示:

    <array>
    <dict>
          <key>Topic</key>
          <string>ABC</string>
          <key>Items</key>
          <array>
            <dict>
              <key>Header</key>
              <string>Data One</string>
              <key>Content</key>
              <array>
                <string>This is how we parse the data.</string>
                <string>This is how we parse the data.</string>
              </array>
            </dict>
            <dict>
              <key>Header</key>
              <string>Data Two</string>
              <key>Content</key>
              <array>
                <string>I am now able to parse the data</string>
                <string>I am now able to parse the data</string>
              </array>
            </dict>
    </array>
    </dict>
    <dict>
          <key>Topic</key>
          <string>ABC</string>
          <key>Items</key>
          <array>
            <dict>
              <key>Header</key>
              <string>Data One</string>
              <key>Content</key>
              <array>
                <string>This is how we parse the data.</string>
                <string>This is how we parse the data.</string>
              </array>
            </dict>
            <dict>
              <key>Header</key>
              <string>Data Two</string>
              <key>Content</key>
              <array>
                <string>I am now able to parse the data</string>
                <string>I am now able to parse the data</string>
              </array>
            </dict>
 </array>
    </dict>
    </array>

我有很多这样的字典。

我正是这样做的:

List<Note> NotesList = (from plist in doc.Root.Element("array").Elements("dict")
           select new Note
           {
                MainTitle = (string)plist.Element("string"),
                ListOfSubTitles = plist.Element("array").Elements("dict")
                                       .Elements("string")
                                       .Select(s => (string)s)
                                       .ToList()
            }).ToList();

这里的MainTitle是ABC,

字幕是数据一,每个字幕都有我需要解析的项目数组?

如何修改上述内容以添加Key Content值。

0 个答案:

没有答案