Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

c# - Parsing inner tag with its value

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...