Reading an XML attribute

Hi guys, just wanted to check whether I’m doing something right. I have an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<iGameData version="9.5">
<GameVersion>1.1</GameVersion>
<DataVersion> 1.0 </DataVersion>
<boosters>
<booster>
<name>Magic</name>
<cost>100</cost>
<duration>3.5</duration>
</booster>
</boosters>
</iGameData>

I’m accessing the attribute this way:

	foreach (XmlNode node in xmlDoc.SelectNodes("iGameData")) {
			string version = node.Attributes.GetNamedItem ("version").Value;
		}

Which works, but do I really have to loop through all nodes to access it (I understand I could break out of the loop once it’s found).

Is there a more direct route to read this value?

Thanks
Ant

You can select any data inside xml document using XPath. For some info please look at w3schools tutorial or MSDN reference.

You can select data with XPath syntax using SelectNodes or SelectSingleNode methods of XmlDocument class. Using the latter, selecting version attribute from your XML would be:

xmlDoc.SelectSingleNode("/iGameData/@version").Value