Coolguy
Coolguy

Reputation: 2285

Get first node name in XML in C#

I have the below in my XML file:

<Resources>
    <a>123.png</a>
</Resources>
<Resources>
    <b>123.png</b>
</Resources>
<Resources>
    <c>123.png</c>
</Resources>

I want to get the first node name of "Resource" which is "a", "b", "c".

But when I do this:

foreach (var downloadFile in downloadFiles.Elements("Resources"))
{
    if (downloadFile.Value != null)
    {
        string fileName = downloadFile.FirstNode.ToString();
     }
}

It will return whole line <a>123.png</a>.

How do I get the firstnode name which is "a"?

Upvotes: 1

Views: 9319

Answers (6)

JohnyL
JohnyL

Reputation: 7142

var xml = XElement.Parse(@"
<root>
    <Resources>
        <a>123.png</a>
    </Resources>
    <Resources>
        <b>123.png</b>
    </Resources>
    <Resources>
        <c>123.png</c>
    </Resources>
</root>");

var all_first =
    xml
    .Elements("Resources") //get all Resources children
    .Select(r => r.FirstNode) //take first node of Resources element
    .OfType<XElement>() //take only XElement (rather XText, XComment etc.)
    .Select(f => f.Name) //finally, take name of the first element
    ;
all_first.ToList().ForEach(Console.WriteLine);

Upvotes: 1

Bouke
Bouke

Reputation: 1577

You need to cast XNode to XElement in order to get the element name. XElement is derived from XNode, but XNode has no Name property, while XElement has.

using System;
using System.Xml.Linq;

namespace StackOverflow
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var xml = XElement.Parse(@"
<root>
    <Resources>
        <a>123.png</a>
    </Resources>
    <Resources>
        <b>123.png</b>
    </Resources>
    <Resources>
        <c>123.png</c>
    </Resources>
</root>");

            var resourceElements = xml.Elements("Resources");
            foreach (var resourceElement in resourceElements)
            {
                // Cast from XNode to XElement
                var desiredElement = resourceElement.FirstNode as XElement; 
                if (desiredElement != null) // In case first node is not an element, but for instance text or comment
                {
                    var elementName = desiredElement.Name;
                    Console.WriteLine(elementName);
                }
            }
            Console.ReadLine();
        }
    }
}

Upvotes: 2

Smaiil
Smaiil

Reputation: 137

You can use Descendants method :

 XElement element = XElement.Parse(xmlString)
        .Descendants()
        .FirstOrDefault();

Giving that xmlString is your XML file.

Upvotes: 0

Keyur Ramoliya
Keyur Ramoliya

Reputation: 1924

You can do it like this:

XmlDocument xml = new XmlDocument();
xml.LoadXml("<Resources><a>123.png</a></Resources>");
Console.WriteLine(xml.DocumentElement.ChildNodes[0].Name);

It is not tested, but it should work.

Upvotes: 0

Barr J
Barr J

Reputation: 10929

You can use an XML document:

XmlDocument oDoc = XmlDocument.Load(yourXmlHere);
XmlNodeList oMainNodes = oDoc.SelectNodes("/Resources");
// Resource's first subnode (a)
foreach(XmlNode oMainNode in oMainNodes)
{
   XmlNode oResourceNode = oMainNode.ChildNodes[0];
   // node a
   string sResourceName = oResourceNode.Name;
}

Upvotes: 0

Ghost4Man
Ghost4Man

Reputation: 1072

You can use the XElement.Name property instead of XElement.ToString().

XElement element = new XElement("a", "123.png");
element.Name.ToString() // returns "a"
element.ToString() // returns "<a>123.png</a>"

Upvotes: 0

Related Questions