Parsing inner tag with its value

守給你的承諾、 提交于 2020-01-10 05:47:05

问题


I have a plist in this format:

<plist version="1.0">
<array>
    <dict>
        <key>Title</key>
        <string>Chapters</string>
        <key>Items</key>
        <array>
            <dict>
                <key>Title</key>
                <string>XYZ</string>

            </dict>
            <dict>
                <key>Title</key>
                <string>ABC</string>

            </dict>
              </array>
    </dict>
    <dict>
        <key>Title</key>
        <string>ChaptersONE</string>
        <key>Items</key>
        <array>
            <dict>
                <key>Title</key>
                <string>ASDF</string>

            </dict>
               </array>
    </dict>
</array>

I have a Class Chapters class with String and List :

i need it like this: Chapters contains list of subtopics like XYZ and ABC and so on... ChaptersONE contains list of subtopics like ASDF and so on...

Now i have tried it like this:

XDocument doc = XDocument.Load(FileName);// plist file name
XElement plist = doc.Element("plist");
XElement array = plist.Element("array");

Chapters chapters = null;
String keyValue = String.Empty;

chapters.listOfItems = new List<Chapters>();

using (XmlReader reader = array.CreateReader())
{
    reader.MoveToContent();
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element)
        {
            if (reader.Name == "dict")
            {
                chapters = new Chapters();
                listOfItems.Add(chapters);
            }
            else if (reader.Name == "key")
            {
                if (!reader.Read())
                {
                    break;
                }
                else if (reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA)
                {
                    keyValue = reader.Value;
                }
            }
            else if (reader.Name == "string")
            {
                if (!reader.Read())
                {
                    break;
                }
                else if (highwayCode != null && reader.NodeType == XmlNodeType.Text || reader.NodeType == XmlNodeType.CDATA)
                {
                    switch (keyValue)
                    {
                        case "Title":
                            chapters.Header = reader.Value;
                            break;
                        case "Items":
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }
}

But i all the Main title (Headers like Chapters and ChaptersOne) and also the subtopics are just assigning to only the string, what am i doing wrong here ?

How to fix acheive this ?

EDIT Chapters should contains list of subtopics like XYZ and ABC and so on... ChaptersONE should contains list of subtopics like ASDF and so on...


回答1:


Yes, there is an easier way:

XDocument doc = XDocument.Load("input.xml");// plist file name

var chapters = (from d in doc.Root.Element("array").Elements("dict")
                select new Chapter
                {
                    Title = (string)d.Element("string"),
                    SubTitles = d.Element("array")
                                 .Elements("dict")
                                 .Elements("string")
                                 .Select(s => (string)s)
                                 .ToList()
                }).ToList();

You didn't show your classes, so I assumed it looks like that:

class Chapter
{
    public string Title { get; set; }
    public List<string> SubTitles { get; set; }
}


来源:https://stackoverflow.com/questions/21980457/parsing-inner-tag-with-its-value

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