如何使用 LINQ 按名称在 XML 中获取元素 [英] How to get elements by name in XML using LINQ

查看:26
本文介绍了如何使用 LINQ 按名称在 XML 中获取元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里选择标题是因为我的问题是我需要获取示例中提到的 Item 节点.我有以下 XML 并且在使用 LINQ 查询它时遇到问题,我之前已经能够解析 XML - 但是我已经坚持了几个小时,希望有人能提供帮助.下面是我的 XML 数据(示例数据):

I've chosen the title here as my problem is I need to get the Item nodes mentioned in the example. I have the following XML and am having problems using LINQ to query it, I've been able to parse XML before - however I've been stuck on this for hours and hope someone can help. Here is my XML data below (example data):

<a:entry
    xmlns:a="http://www.w3.org/2005/Atom">
    <a:id>98765</a:id>
    <info>Data Catalogue</info>
    <data>
        <items>
            <item>
                <id>123456</id>
                <value>Item One</value>
            </item>
            <item>
                <id>654321</id>
                <value>Item Two</value>
            </item>
        </items>
    </data>
    <items>
        <item>
            <id>123456</id>
            <value>Item One</value>
        </item>
        <item>
            <id>654321</id>
            <value>Item Two</value>
        </item>
    </items>
    <a:author>
        <a:name>Catalogue</a:name>
    </a:author>
</a:entry>

我希望能够从 Items 下的 Item XML 标签中提取 ID,但是在数据下有一个带有 Item 条目的 Items 标签我根本不想要这些节点 - 我想要 root/items/id/id 如果这被表示为路径.我已经用 LINQ 尝试了我所知道的一切,所以如果有人可以提供帮助,请注意尽管这是基于系统的示例数据 - 格式无法更改,因此这不是一个可接受的解决方案.
我似乎无法确定我哪里出错了 - 我尝试的每个 LINQ 表达式都没有返回任何内容,我认为命名空间是一个问题,并试图将其集成,但我正在兜圈子.
解决方案必须适用于 Silverlight 和 C#

I want to be able to extract the ID from the Item XML tag under Items, however there is an Items Tag with Item entries under data I DO NOT want these nodes at all - I want root/items/id/id if this were expressed as path. I've tried everything I know with LINQ so if someone could help, things to note although this is sample data it is based on the system - the format cannot be changed so that is not an acceptable solution.
I can't seem to determine where I'm going wrong - every LINQ expression I try returns nothing, I think the namespace is an issue and have tried to integrate this but I'm going in circles.
Solution must work in Silverlight and C#

我尝试了以下方法:

IEnumerable<XElement> nodes =
    element.Elements().Where(e => e.Name.LocalName == "items")

然而,这让我得到了所有的项目";包括数据"下的那些我不想要那些.

However this gets me all the "items" including the ones under "data" I don't want those.

如果我对 XML 执行以下操作,我会看到显示的元素名称:

If I do the following on my XML I do see the Names of the Elements displayed:

XElement element = XElement.Parse(data);
foreach (XElement node in element.Elements())
{
  MessageBox.Show(node.Name.LocalName);
}

但是,当我这样做时,我根本看不到项目下的节点名称 - 我已经检查了 XElement 并且它确实有节点,当我输出其上方的名称时,项目"会显示与信息和 ID 一起显示!

However when I do this I cannot see the node names under items at all - I've checked the XElement and it does have the node and when I output the names above it "items" shows up along with info and id!

foreach (XElement node in element.Elements("items"))
{
  MessageBox.Show(node.Name.LocalName);
}

推荐答案

假设 element 是你的 元素:

Assuming element is your <a:entry> element:

var ids = element.Element("items")
                 .Elements("item")
                 .Select(item => item.Element("id").Value);

ElementElements 方法只返回直接子元素,而不是所有子元素,因此它不返回 元素在

The Element and Elements methods return only direct children, not all descendants, so it doesn't return the <items> element which is under <data>

这篇关于如何使用 LINQ 按名称在 XML 中获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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