How to get elements by name in XML using LINQ

61,261

Solution 1

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>

Solution 2

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); 
Share:
61,261
RoguePlanetoid
Author by

RoguePlanetoid

Software Developer for over thirty years from BASIC, VB.NET to C# and a Web Developer for over fifteen years. Was a Windows Phone 8.1 and Windows 8.1 Developer including creating Applications and Tutorials. Now an ASP.NET Web Developer and Universal Windows Platform Developer in spare time Skills: XML, HTML, XHTML, CSS, XAML, ASP, VB (Classic and .NET), C#, Silverlight, WPF, Windows Phone, Win RT, UWP, ASP.NET MVC, Razor, MS SQL

Updated on April 20, 2021

Comments

  • RoguePlanetoid
    RoguePlanetoid about 3 years

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