将 XML 转换为 C# 字典

发布于 2024-10-30 22:02:42 字数 1670 浏览 1 评论 0 原文

我有一个 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>。字典中的每个条目都是一个 元素,其中字典的键是 节点,字典的值是 List 元素。 Item>,其中 Item 对象由 itemidnewused 组成。

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

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

因此,上面的代码将为我创建一个 Item 列表,但是如何访问每个 节点code> 节点?同样, 是键,每个方案下的 List 是我的 Dictionary 的值。

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

I've got an XML response that looks like:

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

I've got to convert this XML response into a Dictionary<string, List<Item>>. Each entry in the dictionary is a <scheme> element, where the Dictionary's key is the <name> node, and the Dictionary's value is the List<Item>, where the Item object is comprised of itemid, new, and used.

So far I've got code that looks like:

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

So the code I've got above will create me a list of Item, but how do I get access to the <name> node in each <scheme> node? Again, the <name> is the key, and the List<Item> under each scheme is the value for my Dictionary.

Any suggestions on a different way to do this??!? I'm definitely not doing this right..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

同尘 2024-11-06 22:02:42

尝试这个方法:

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()
                           );

Try this approach:

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()
                           );
倾城泪 2024-11-06 22:02:42

它必须是一本字典吗?

我问的原因是,您可以创建一个具有项目属性的项目类。
然后创建一个方案类,该类具有名为“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 为您进行反序列化。

当然,如果你必须查字典的话,这是行不通的。

Does it HAVE to be a dictionary?

The reason I'm asking is, you could create an item class that has your item properties.
Then create a scheme class that has a property called "Name" as well as a List property.

You could then use .NET deserialization to create a list of schemes, each scheme having a list of items.

Something like:

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

Then follow a tutorial like this one: http://sharpertutorials.com/serialization/
And let .Net do the deserialization for you.

Granted, this won't work if you have to do a dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文