Getting a value by XML Nodes by their Name.

vishalneeraj-24503
Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 298
To get all <Name> nodes,we will use XPath expression /Names/Name.The first slash means that the <Names> node must be a root node.
using System.Xml;
string str_val = "<Names><Name><FirstName>Vishal</FirstName><LastName>Kumar</LastName></Name><Name><FirstName>Neeraj</FirstName><LastName>Kumar</LastName></Name></Names>";
XmlDocument xml_doc = new XmlDocument();
xml_doc.LoadXml(str_val);

XmlNodeList xml_list = xml_doc.SelectNodes("/Names/Name");
foreach (XmlNode xn in xml_list)
{
string emp_first_name = xn["FirstName"].InnerText;
string emp_last_name = xn["LastName"].InnerText;
Response.Write(string.Format("FirstName: {0} LastName: {1}", emp_first_name, emp_last_name) + "<br/>");
}


Output:-
FirstName: Vishal LastName: Kumar
FirstName: Neeraj LastName: Kumar

Comments or Responses

Login to post response