Getting attribute value of an XML Document using C#

Viewed 112217

Suppose I have the following XML Document.

<reply success="true">More nodes go here</reply>

How to get the value of the attribute success, which in this case would be the string "true".

7 Answers

If attribute value returns null or empty, try this.

     string x="<node1 id='12345'><node2 Name='John'></node2><node3 name='abc'></node3></node1>";
        XmlDocument xml = new XmlDocument();
        xml.LoadXml(x);
        var node = xml.GetElementsByTagName("node3");
        xml = new XmlDocument();
        xml.LoadXml(nodes[0].OuterXml);
        XmlElement root = xml.DocumentElement;
        String requiredValue = root.GetAttribute("name");  
    returns "abc";      
Related