使用Dictionary< string,List< MyObject>>进行Xml解析. [英] Xml Parsing with Dictionary<string, List<MyObject>>

查看:108
本文介绍了使用Dictionary< string,List< MyObject>>进行Xml解析.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从事Windows Phone 8应用程序开发.

I am working on Windows Phone 8 app development.

我有一个如下所示的xml:

i have a xml like the below:

<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 // list of title 

  ---ID

  ---Desc

  ---Boolean Value
 MainTitle 

   --SubTitle //list of values

  ---ID

  ---Desc

  ---Boolean Value

现在在第一个屏幕中,我正在显示所有主要标题

Now in 1st screen i am displaying all the Main Titles

现在在第一个屏幕上单击主图块"时,我需要该主标题的所有值.

Now on click of Main tile in 1st screen i need all the values of that main title.

那么我该如何存储这些Dictionary<string, List<MyObject>>?

So how can i store these a Dictionary<string, List<MyObject>> ?

编辑

我已经尝试过了:

var dict = (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(),
                          })
                         .ToDictionary(a => a.MainTitle, a => a.ListOfSubTitles);

但是这里它也将Desc标签值也存储在ListOfSubTitles

But here its also storing the Desc tag values also in the ListOfSubTitles

推荐答案

我建议使用辅助方法.第一个方法是使用奇怪的xml数据格式创建值的字典(它使用key元素值作为字典条目键,并使用下一个节点作为字典条目值):

I suggest to use helper methods. First one is to create dictionary of values from your strange xml data format (it uses key element value as dictionary entry key, and next node as dictionary entry value):

static Dictionary<string, XElement> GetValues(XElement dict)
{
    return dict.Elements("key")
               .ToDictionary(k => (string)k, k => (XElement)k.NextNode);
}

第二个是解析MyObject:

static MyObject ParseMyObject(XElement dict)
{
    var values = GetValues(dict);

    return new MyObject
    {
        MainTitle = (string)values["Title"],
        ListOfSubTitles = values["ID"].Elements().Select(s => (string)s).ToList()
    };
}

所有解析如下:

XDocument xdoc = XDocument.Load(path_to_xml);
var result = 
    xdoc.Root.Elements("dict")
        .Select(GetValues)
        .ToDictionary(v => (string)v["MainTitle"],
                      v => v["SubTopics"]
                              .Elements("dict").Select(ParseMyObject).ToList());

解析结果:

{
  "Data": [
    {
      MainTitle: "Miscellaneous",
      ListOfSubTitles: [ "CD1", "CD2", "CD3", "CD4" ]
    },
    {
      MainTitle: "Miscellaneous One",
      ListOfSubTitles: [ "DDC1", "DDC2", "DDC3", "DDC4", "DDC5" ]
    }
  ],
  "Data Two": [
    {
      MainTitle: "Goblins",
      ListOfSubTitles: [ "SSD1", "SS2", "SS3", "SS4" ]
    },
    {
      MainTitle: "Tracks",
      ListOfSubTitles: [ "ADC1", "ADC2", "ADC3", "ADC4", "DDC5" ]
    }
  ]
}

这篇关于使用Dictionary&lt; string,List&lt; MyObject&gt;&gt;进行Xml解析.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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