Reading XML values using XmlNode and InnerText in C#

This example shows you how to read xml file node value in c#. By using XmlDocument we can load xml file(xml.Load(xmlfilepath)) or xml string(xml.LoadXml(xmlstring)), then xml.SelectNodes() will return the XmlNodeList. foreach of XmlNodeList will run loop on each XmlNode and XmlNod[NodeName].InnerText will return the value of the node.

Input XML Strin

private string GetXML() { return @"<?xml version='1.0' encoding='UTF-8' ?> <EmployeeList> <Employee> <Name>James</Name> <DOJ>10/05/2014</DOJ> </Employee> <Employee> <Name>Smith</Name> <DOJ>06/03/2013</DOJ> </Employee> <Employee> <Name>Williams</Name> <DOJ>02/01/2013</DOJ> </Employee> </EmployeeList>"; }

Reading XML value

protected void Page_Load(object sender, EventArgs e) { string strXML = GetXML(); XmlDocument xml = new XmlDocument(); xml.LoadXml(strXML); XmlNodeList xNodeList = xml.SelectNodes("/EmployeeList/Employee"); foreach (XmlNode xNode in xNodeList) { string employeeName = xNode["Name"].InnerText; string DOJ = xNode["DOJ"].InnerText; Response.Write("Employee Name: " + employeeName + "<br/>DOJ: " + DOJ + "<br/><br/>"); } }

reading xml using c#