【问题标题】:Convert XML to C# Dictionary将 XML 转换为 C# 字典
【发布时间】:2011-04-07 19:16:48
【问题描述】:

我有一个如下所示的 XML 响应:

<?xml version="1.0" encoding="utf-8"?>
<response>
  <scheme>
    <name>name_1</name>
    <items>
      <item>
        <itemid>testid_1</itemid>
        <new></new>
        <used></used>
      </item>
      <item>
        <itemid>testid_2</itemid>
        <new></new>
        <used></used>
      </item>
    </items>
  </scheme>
  <scheme>
    <name>name_2</name>
    <items>
      <item>
        <itemid>testid_3</itemid>
        <new></new>
        <used></used>
      </item>
    </items>
  </scheme>
</response>

我必须将此 XML 响应转换为 Dictionary&lt;string, List&lt;Item&gt;&gt;。字典中的每个条目都是一个&lt;scheme&gt; 元素,其中字典的键是&lt;name&gt; 节点,字典的值是List&lt;Item&gt;,其中Item 对象由itemidnew 组成,和used

到目前为止,我的代码如下所示:

var items =
            from item in itemsXml.Descendants("scheme")
            select new Item
            {
                ItemId = item.Element("itemid").Value,
                New = item.Element("new").Value,
                Used = item.Element("used").Value,
            };

所以我上面的代码将为我创建一个项目列表,但是我如何访问每个&lt;scheme&gt; 节点中的&lt;name&gt; 节点?同样,&lt;name&gt; 是键,每个方案下的 List&lt;Item&gt; 是我的字典的值。

对不同的方法有什么建议吗??!?我绝对没有这样做..

【问题讨论】:

    标签: xml linq dictionary


    【解决方案1】:

    试试这个方法:

    var dict = xml.Elements("scheme")
                  .ToDictionary(e => e.Element("name").Value,
                                e => e.Descendants("item")
                                      .Select(item => new Item()
                                      {
                                          ItemId = item.Element("itemid").Value,
                                          New = item.Element("new").Value,
                                          Used = item.Element("used").Value
                                      }).ToList()
                               );
    

    【讨论】:

    • 我尝试这样做,但它 xml.Elements("scheme") 为 NULL。需要注意的是,xml 元素的类型是 XDocument。
    • 如果我吐出 XDocument 的内容,我得到: category_rrres11040714675342229509419
    • 我已将初始 xml.Elements("scheme") 更改为 xml.Descendants("scheme") 并修复了所有问题。谢谢!
    • @Shafique 对于XDocument,您需要访问Root 属性:xml.Root.Elements("scheme")...
    • @AhmadMageed 你能帮我解决这个问题吗stackoverflow.com/questions/22146447/…
    【解决方案2】:

    必须是字典吗?

    我问的原因是,您可以创建一个具有您的项目属性的项目类。 然后创建一个具有名为“Name”的属性以及 List 属性的方案类。

    然后您可以使用 .NET 反序列化来创建方案列表,每个方案都有一个项目列表。

    类似:

    public class Response
    {
        public IList<Scheme> Schemes {get; set;}
    }
    
    public class Scheme
    {
        public string Name {get; set;}
        public IList<Item> Items {get; set;}
    }
    
    public class Item
    {
        public string ItemId {get; set;}
        public bool New {get; set;}
        public bool Used {get; set;}
    }
    

    然后按照这样的教程进行操作:http://sharpertutorials.com/serialization/ 并让 .Net 为您进行反序列化。

    当然,如果你必须做字典,这将不起作用。

    【讨论】:

    • 序列化是否“有创意”?还是我错过了什么?
    • 不,我觉得这很无聊。
    • 嗯,我认为想出一些不是 ToDictionary 答案的股票的东西是有创意的,而且仍然可能有用。
    • 不幸的是,我确实需要字典>
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 2021-03-28
    • 2012-12-06
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多