views:

22628

answers:

5
+4  A: 

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
Thanks mate! You pointed me in the right direction :)
Alex
+9  A: 
    XmlNodeList elemList = doc.GetElementsByTagName(...);
    for (int i = 0; i < elemList.Count; i++)
    {
        string attrVal = elemList[i].Attributes["SuperString"].Value;
    }  

Is this helps?

ArsenMkrt
Yes. I did something similar after jerryjvl's post. Thanks!
Alex
+11  A: 

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;
Greg
I will definitely check this out. Thank you!
Alex
I changed the XPath to be correct by adding the name of the root node, sorry it was incorrect before.
Greg
+1 Without a doubt the best way.
Fábio Antunes
+1  A: 

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
A: 

Big thanks ArsenMkrt. You solved my problem.