How to get elements by name in XML using LINQ

2019-02-12 10:35发布

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>

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#

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:

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!

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

2条回答
疯言疯语
2楼-- · 2019-02-12 11:09

Assuming element is your <a:entry> element:

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>

查看更多
来,给爷笑一个
3楼-- · 2019-02-12 11:16

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!

XNamespace ns = "http://example.org/namespace";
var ids = element.Element(ns + "items") 
                 .Elements("item") 
                 .Select(item => item.Element("id").Value); 
查看更多
登录 后发表回答