Thursday, 29 September 2011

Parse an XML string/file for a single node in C#


string myXMl = "<Employees>" +
                 "<Employee ID='1' Name='Ravi'" +
                      "Address='Pandav Nagar'" +
                      "City='New Delhi' Zip='110092'>" +
                 "</Employee>" +
               "</Employees>";

XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(myXMl);

XmlNode xmlNode = xDoc.SelectSingleNode("/Employees/Employee[@ID='1']");

string Name =
xmlNode.Attributes["Name"].Value;//Outputs: Ravi
string Address =
xmlNode.Attributes["Address"].Value;//Outputs: Pandav Nagar
string City =
xmlNode.Attributes["City"].Value;//Outputs: New Delhi
string Zip =
xmlNode.Attributes["Zip"].Value; //Outputs: 110092

No comments:

Post a Comment