Pasing nested array using LINQ to XML

被刻印的时光 ゝ 提交于 2019-12-12 01:37:33

问题


I have xml like the below:

    <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>

I have many dict like this.

This is how i am doing it:

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();

Here MainTitle is ABC,

Subtitle is Data One and for each subtitle we have array of items which i need to parse?

How can i modify the above to add the Key Content values as well.

来源:https://stackoverflow.com/questions/22502518/pasing-nested-array-using-linq-to-xml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!