简体   繁体   English

如何使用 LINQ 按名称在 XML 中获取元素

[英]How to get elements by name in XML using LINQ

I've chosen the title here as my problem is I need to get the Item nodes mentioned in the example.我在这里选择标题是因为我的问题是我需要获取示例中提到的 Item 节点。 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.我有以下 XML 并且在使用 LINQ 查询它时遇到问题,我之前已经能够解析 XML - 但是我已经坚持了几个小时,希望有人能提供帮助。 Here is my XML data below (example data):下面是我的 XML 数据(示例数据):

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

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

I have tried the following:我尝试了以下方法:

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.但是,这让我获得了所有“项目”,包括我不想要的“数据”下的项目。


If I do the following on my XML I do see the Names of the Elements displayed:如果我对 XML 执行以下操作,我确实会看到显示的元素名称:

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

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!但是,当我这样做时,我根本看不到项目下的节点名称 - 我检查了 XElement,它确实有节点,当我输出上面的名称时,“项目”与信息和 ID 一起显示!

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

Assuming element is your <a:entry> element:假设element是您的<a:entry>元素:

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

The Element and Elements methods return only direct children, not all descendants, so it doesn't return the <items> element which is under <data> ElementElements方法仅返回直接子项,而不是所有子项,因此它不会返回<data>下的<items>元素

I had a blank Namespace Declaration in my XML I hadn't noticed once I added this into my code it worked - forgot LINQ is very NameSpace oriented!我的 XML 中有一个空白的命名空间声明,一旦我将它添加到我的代码中,我就没有注意到它起作用了 - 忘记了 LINQ 是非常面向命名空间的!

XNamespace ns = "http://example.org/namespace";
var ids = element.Element(ns + "items") 
                 .Elements("item") 
                 .Select(item => item.Element("id").Value); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM