Menu ▼

Posted By :
Total Views : 5416

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' ?>

Reading XML value

protected void Page_Load(object sender, EventArgs e)
    string strXML = GetXML();

    XmlDocument xml = new XmlDocument();

    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#