LINQ to XML 组查询

发布于 2024-11-15 05:38:26 字数 611 浏览 2 评论 0 原文

如果我有像这样的 xml:

<List>
  <Item>
     <Type>Type1</Type>
  </Item>
  <Item>
     <Type>Type1</Type>
  </Item>
  <Item>
     <Type>Type2</Type>
  </Item>
</List>

我基本上想以某种方式创建一个字典或列表来表示这一点。这是我到目前为止所拥有的:

var grouped = (from p in _xmlDoc.Descendants("Item")
               group p by p.Element("Blah").Value into Items
               select Items);

我将如何选择它以便创建一个 Dictionary> 其中 key 是分组的名称和 ("Type1" 或 "Type2 ”)。

If I have xml like:

<List>
  <Item>
     <Type>Type1</Type>
  </Item>
  <Item>
     <Type>Type1</Type>
  </Item>
  <Item>
     <Type>Type2</Type>
  </Item>
</List>

I basically want to somehow create a dictionary or list to repesent this. Here's what I have so far:

var grouped = (from p in _xmlDoc.Descendants("Item")
               group p by p.Element("Blah").Value into Items
               select Items);

How would I select it so that I create a Dictionary<string, List<Item>> where key is the name of the grouping and ("Type1" or "Type2").

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

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

发布评论

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

评论(3

七分※倦醒 2024-11-22 05:38:26

如果您不需要随机访问这些项目,请使用查找< /code>,这就是它的用途:

var lookup = _xmlDoc.Descendants("Item")
    .ToLookup(e => (string)e.Element("Blah"));

否则,如果您需要随机访问,则将它们放入字典中:

var dict = _xmlDoc.Descendants("Item")
    .GroupBy(e => (string)e.Element("Blah"))
    .ToDictionary(g => g.Key, g => g.ToList());

If you don't need random access to the items, use a Lookup, that's what it's there for:

var lookup = _xmlDoc.Descendants("Item")
    .ToLookup(e => (string)e.Element("Blah"));

Otherwise, if you need random access, then throw them into a dictionary:

var dict = _xmlDoc.Descendants("Item")
    .GroupBy(e => (string)e.Element("Blah"))
    .ToDictionary(g => g.Key, g => g.ToList());
浅紫色的梦幻 2024-11-22 05:38:26
XDocument document = XDocument.Parse(xml);

var query = (from item in document.Descendants("item")
             select new
             {
                 Key = (int)item.Value,
                 Value = (string)item.Descendants("type").Value
             }).ToDictionary(pair => pair.Key, pair => pair.Value);
XDocument document = XDocument.Parse(xml);

var query = (from item in document.Descendants("item")
             select new
             {
                 Key = (int)item.Value,
                 Value = (string)item.Descendants("type").Value
             }).ToDictionary(pair => pair.Key, pair => pair.Value);
柏林苍穹下 2024-11-22 05:38:26
var grouped = (from p in _xmlDoc.Descendants("Item")
               group p by p.Element("Blah").Value into Items
               select Items).ToDictionary(g=>g.Key, g=>g.Value);

类似这样,您必须检查正确的语法,但基本上当您按某些内容进行分组时,您也有键和值,并且您可以使用 ToDictionary 方法将其转换为字典。

var grouped = (from p in _xmlDoc.Descendants("Item")
               group p by p.Element("Blah").Value into Items
               select Items).ToDictionary(g=>g.Key, g=>g.Value);

Something like that, you'll have to check the correct syntax, but basically when you group by something, you have the Key and Value too, and you can use ToDictionary method to transform this into a dictionary.

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