带有嵌套字典和数组的 Linq to XML [英] Linq to XML with nested Dictionary and Array

查看:15
本文介绍了带有嵌套字典和数组的 Linq to XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理 Windows Phone 8 应用程序:

I am Working on Windows Phone 8 application:

我有一个这样的文件:

<array>
    <dict>
        <key>SubTopics</key>
        <array>
            <dict>
                <key>ID</key>
                <array>
                    <string>CD1</string>
                    <string>CD2</string>
                    <string>CD3</string>
                    <string>CD4</string>    
                </array>
                <key>Title</key>
                <string>Miscellaneous</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
            <dict>
                <key>ID</key>
                <array>
                    <string>DDC1</string>
                    <string>DDC2</string>
                    <string>DDC3</string>
                    <string>DDC4</string>
                    <string>DDC5</string>
                </array>
                <key>Title</key>
                <string>Miscellaneous One</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
      </array>
      <key>MainTitle</key>
      <string>Data</string>
    </dict>
    <dict>
        <key>SubTopics</key>
        <array>
            <dict>
                <key>ID</key>
                <array>
                    <string>SSD1</string>
                    <string>SS2</string>
                    <string>SS3</string>
                    <string>SS4</string>    
                </array>
                <key>Title</key>
                <string>Goblins</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
            <dict>
                <key>ID</key>
                <array>
                    <string>ADC1</string>
                    <string>ADC2</string>
                    <string>ADC3</string>
                    <string>ADC4</string>
                    <string>DDC5</string>
                </array>
                <key>Title</key>
                <string>Tracks</string>
                <key>Desc</key>
                <string> this is just a text</string>
                <key>HasItems</key>
                <true/>
            </dict>
      </array>
      <key>MainTitle</key>
      <string>Data Two</string>
    </dict>
</array>

如何解析这个?

是这样的:

MainTitle 

   --SubTitle

  ---ID

  ---Desc

  ---Boolean Value
 MainTitle 

   --SubTitle

  ---ID

  ---Desc

  ---Boolean Value

基本上是一个包含键和数组值列表的字典列表.

Basically a List of Dictionary with Key and Array list of values.

我尝试过这样但不完整:

I have tried like this but its not complete:

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

推荐答案

好的,这里有一个可以解析你的 xml 的小类:

Ok, here is a little class that can parse your xml:

public class Parser
{
    public List<Dictionary<string, object>> Parse(XElement root)
    {
        var result = new List<Dictionary<string, object>>();

        foreach (var e in root.Elements())
        {
            if (e.Name == "dict")
            {
                result.Add(ParseDict(e));
            }
        }

        return result;
    }

    private Dictionary<string, object> ParseDict(XElement element)
    {
        var dict = new Dictionary<string, object>();

        foreach (var subelement in element.Elements())
        {
            if (subelement.Name == "key")
            {
                dict.Add(subelement.Value, ParseValue(subelement.ElementsAfterSelf().First()));        
            }
        }

        return dict;
    }

    private object ParseValue(XElement valueElement)
    {
        if (valueElement.Name == "string")
        {
            return valueElement.Value;
        }

        if (valueElement.Name == "array")
        {
            return new List<object>(valueElement.Elements().Select(e => ParseValue(e)));
        }

        if (valueElement.Name == "dict")
        {
            return ParseDict(valueElement);
        }

        if (valueElement.Name == "true")
        {
            return true;
        }

        if (valueElement.Name == "false")
        {
            return false;
        }

        return null;
    }
}

它是这样使用的:

        var parser = new Parser();
        var doc = XDocument.Load(<path to xml file>);

        var result = parser.Parse(doc.Root);

解析器非常粗糙,对xml做出假设.正如之前的评论中指出的那样,这不是像这样使用 xml 的最佳方式,其中元素的位置很重要.此外,在解析器中使用对象"也不是一个好的解决方案,但如果您想避免这种情况,解析器会变得更先进.

The parser is very crude and makes assumptions about the xml. And as pointed out in earlier comments, it is not the best way to use xml like this, where position of elements has significance. Also the use of "object" in the parser isn't a good solution, but the parser gets a lot more advanced if you want to avoid that.

这篇关于带有嵌套字典和数组的 Linq to XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆