I’ve been taking XML drills through Inner Working’s online certification. New ways of handling XML data have made it easier than ever. However, there are some pitfalls when querying XML. Below is an example. If you want to get and “ID” attribute from a document element, here is what you might do:
[sourcecode language='csharp']
string documentID;
//Correct: Referencing the “Document Element” variable
documentID = xmldoc.DocumentElement.Attributes["id"].InnerXml;
//Correct: Selecting the “Document” node”
documentID = xmldoc.SelectSingleNode(“document”).Attributes["id"].InnerXml;
//Wrong: Selecting the “Document” node”
documentID = xmldoc.DocumentElement.SelectSingleNode(“document”).Attributes["id"].InnerXml;
[/sourcecode]
Below is the full source
[sourcecode language='csharp']
System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream (“WindowsFormsApplication1.resources.XMLFile.xml”);
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(s);
s.Close();
string ItemID = xmldoc.SelectSingleNode(“document”).Attributes["id"].InnerXml;
Console.WriteLine(ItemID);
[/sourcecode]
Stuart Zahn is a software developer, blogger, and passionate about personal development. I believe there is intrinsic purpose behind all my work. As a programmer, my purpose is to help your business by delivering better software. I just call it creative indulgence. I try to put personal development and character above career. So, if you want to discuss personal development, partnerships, or job opportunities, please feel free to send me a message anytime.No related posts.