We can write the following code to read the value of the attribute “id” of the first customer –
// Load the xml file
XmlDocument doc = new XmlDocument();
doc.LoadXml(Resources.XMLFile);
// Read the first customer element
XmlNode customer = doc.DocumentElement.SelectSingleNode("customer");
// Initialize the reader using the customer node
// from where need to read attributes
XmlNodeReader reader = new XmlNodeReader(customer);
reader.Read();
// Read the attribute named - "id"
string id = reader.GetAttribute("id");
Console.WriteLine(id);
The output of this program is "123". I find it helpful during working one of my project and I think it will also help you. You can also read this value very easily if you use LINQ in C# 3.0.
2 comments:
Nice article.
Visit this link to learn about
XML Namespaces -
http://www.w3schools.com/XML/xml_namespaces.asp
Visit my blog - http://sadique-sadiqulislam.blogspot.com/
Nice article.
To learn about XML Namspace visit -
http://www.w3schools.com/XML/xml_namespaces.asp
To learn XML data manipulation in sql server 2005 visit -
http://sadique-sadiqulislam.blogspot.com/
Post a Comment