How to read attribute value from XmlNode in C#?
Solution 1:
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:
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.");
}