解析内部标签及其值

Parsing inner tag with its value(解析内部标签及其值)
本文介绍了解析内部标签及其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这种格式的 plist:

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>

我有一个带有 String 和 List 的 Class Chapters 类:

I have a Class Chapters class with String and List :

我需要这样的:章节包含诸如 XYZ 和 ABC 等子主题的列表......ChaptersONE 包含 ASDF 等子主题列表...

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

但是我所有的主标题(像 Chapters 和 ChaptersOne 这样的标题)以及子主题都只是分配给字符串,我在这里做错了什么?

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 ?

如何解决这个问题?

编辑章节应包含 XYZ 和 ABC 等子主题列表...ChaptersONE 应包含 ASDF 等子主题列表...

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

推荐答案

是的,有一个更简单的方法:

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

这篇关于解析内部标签及其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)