How to read attribute value from XmlNode in C#?


C# Problem Overview

Suppose I have a XmlNode and I want to get the value of an attribute named "Name". How can I do that?

XmlTextReader reader = new XmlTextReader(path);

XmlDocument doc = new XmlDocument();
XmlNode node = doc.ReadNode(reader);

foreach (XmlNode chldNode in node.ChildNodes)
     **//Read the attribute Name**
     if (chldNode.Name == Employee)
         if (chldNode.HasChildNodes)
             foreach (XmlNode item in node.ChildNodes)


XML Document:

    <Employee Name ="TestName">

C# Solutions

Solution 1 - C#

Try this:

string employeeName = chldNode.Attributes["Name"].Value;

Edit: As pointed out in the comments, this will throw an exception if the attribute doesn't exist. The safe way is:

var attribute = node.Attributes["Name"];
if (attribute != null){
    string employeeName = attribute.Value;
    // Process the value here

Solution 2 - C#

To expand Konamiman's solution (including all relevant null checks), this is what I've been doing:

if (node.Attributes != null)
   var nameAttribute = node.Attributes["Name"];
   if (nameAttribute != null) 
      return nameAttribute.Value;

   throw new InvalidOperationException("Node 'Name' not found.");

Solution 3 - C#

you can loop through all attributes like you do with nodes

foreach (XmlNode item in node.ChildNodes)
    // node stuff...

    foreach (XmlAttribute att in item.Attributes)
        // attribute stuff

Solution 4 - C#

If you use chldNode as XmlElement instead of XmlNode, you can use

var attributeValue = chldNode.GetAttribute("Name");

The return value will just be an empty string, in case the attribute name does not exist.

So your loop could look like this:

XmlDocument document = new XmlDocument();
var nodes = document.SelectNodes("//Node/N0de/node");

foreach (XmlElement node in nodes)
    var attributeValue = node.GetAttribute("Name");

This will select all nodes <node> surrounded by <Node><N0de></N0de><Node> tags and subsequently loop through them and read the attribute "Name".

Solution 5 - C#

if all you need is the names, use xpath instead. No need to do the iteration yourself and check for null.

string xml = @"
	<Employee name=""an"" />
	<Employee name=""nobyd"" />

var doc = new XmlDocument();


var names = doc.SelectNodes("//Employee/@name");

Solution 6 - C#



to get the value.

Solution 7 - C#

You can also use this;

string employeeName = chldNode.Attributes().ElementAt(0).Name

Solution 8 - C#

Yet another solution:

string s = "??"; // or whatever

if (chldNode.Attributes.Cast<XmlAttribute>()
                       .Select(x => x.Value)
   s =  xe.Attributes[attributeName].Value;

It also avoids the exception when the expected attribute attributeName actually doesn't exist.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshish AshuView Question on Stackoverflow
Solution 1 - C#KonamimanView Answer on Stackoverflow
Solution 2 - C#Ari RothView Answer on Stackoverflow
Solution 3 - C#balexandreView Answer on Stackoverflow
Solution 4 - C#Marco7757View Answer on Stackoverflow
Solution 5 - C#an phuView Answer on Stackoverflow
Solution 6 - C#rahulView Answer on Stackoverflow
Solution 7 - C#cell-inView Answer on Stackoverflow
Solution 8 - C#TaWView Answer on Stackoverflow