Since you are using C # 4.0, you can use linq-to-xml as follows:
XDocument xdoc = XDocument.Load(@"C:\Tmp\your-xml-file.xml"); foreach (var item in xdoc.Descendants("a")) { Console.WriteLine(item.Attribute("id").Value); }
Should give you the element a
no matter where it is in the hierarchy.
From your comment, for code that uses only the XmlDocument and XmlElement classes, the equivalent code would be:
XmlDocument dd = new XmlDocument(); dd.Load(@"C:\Tmp\test.xml"); XmlElement theElem = ((XmlElement)dd.GetElementsByTagName("data")[0]); // ^^ this is your target element foreach (XmlElement item in theElem.GetElementsByTagName("a"))//get the <a> { Console.WriteLine(item.Attributes["id"].Value);//get their attributes }
source share