84

How can I read an XML attribute using C#'s XmlDocument?

I have an XML file which looks somewhat like this:

<?xml version="1.0" encoding="utf-8" ?>
<MyConfiguration xmlns="http://tempuri.org/myOwnSchema.xsd" SuperNumber="1" SuperString="whipcream">
    <Other stuff />
</MyConfiguration> 

How would I read the XML attributes SuperNumber and SuperString?

Currently I'm using XmlDocument, and I get the values in between using XmlDocument's GetElementsByTagName() and that works really well. I just can't figure out how to get the attributes?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Alex
  • 75,813
  • 86
  • 255
  • 348

7 Answers7

121
XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
    string attrVal = elemList[i].Attributes["SuperString"].Value;
}
radbyx
  • 9,352
  • 21
  • 84
  • 127
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • thank you so much. it really works and it doesn't need any paths and nothing. simply superb!! – Nani Oct 19 '16 at 10:32
91

You should look into XPath. Once you start using it, you'll find its a lot more efficient and easier to code than iterating through lists. It also lets you directly get the things you want.

Then the code would be something similar to

string attrVal = doc.SelectSingleNode("/MyConfiguration/@SuperNumber").Value;

Note that XPath 3.0 became a W3C Recommendation on April 8, 2014.

Aleks Andreev
  • 7,016
  • 8
  • 29
  • 37
Greg
  • 2,229
  • 1
  • 16
  • 20
8

I have an Xml File books.xml

<ParameterDBConfig>
    <ID Definition="1" />
</ParameterDBConfig>

Program:

XmlDocument doc = new XmlDocument();
doc.Load("D:/siva/books.xml");
XmlNodeList elemList = doc.GetElementsByTagName("ID");     
for (int i = 0; i < elemList.Count; i++)     
{
    string attrVal = elemList[i].Attributes["Definition"].Value;
}

Now, attrVal has the value of ID.

kratenko
  • 7,354
  • 4
  • 36
  • 61
siva
  • 81
  • 1
  • 1
8

You can migrate to XDocument instead of XmlDocument and then use Linq if you prefer that syntax. Something like:

var q = (from myConfig in xDoc.Elements("MyConfiguration")
         select myConfig.Attribute("SuperString").Value)
         .First();
Matt Sherman
  • 8,298
  • 4
  • 37
  • 57
5

XmlDocument.Attributes perhaps? (Which has a method GetNamedItem that will presumably do what you want, although I've always just iterated the attribute collection)

jerryjvl
  • 19,723
  • 7
  • 40
  • 55
1

Assuming your example document is in the string variable doc

> XDocument.Parse(doc).Root.Attribute("SuperNumber")
1
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
1

If your XML contains namespaces, then you can do the following in order to obtain the value of an attribute:

var xmlDoc = new XmlDocument();

// content is your XML as string
xmlDoc.LoadXml(content);

XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());

// make sure the namespace identifier, URN in this case, matches what you have in your XML 
nsmgr.AddNamespace("ns", "urn:oasis:names:tc:SAML:2.0:protocol");

// get the value of Destination attribute from within the Response node with a prefix who's identifier is "urn:oasis:names:tc:SAML:2.0:protocol" using XPath
var str = xmlDoc.SelectSingleNode("/ns:Response/@Destination", nsmgr);
if (str != null)
{
    Console.WriteLine(str.Value);
}

More on XML namespaces here and here.

Voicu
  • 16,921
  • 10
  • 60
  • 69