Tuesday, October 4, 2016

Read and Save XML using XmlDocument

XmlDocument :


XmlDocument represents an XML document. With XmlDocument class 
we can load XML into the DOM (Document Object Model) and do manipulations
like add, edit, remove, navigate, find and validate nodes. If we just want to read
XML data and no modifications required for the XML document, we can use
XmlReader Class and if we require modification of document structure we can use
XmlDocument Class.

Below I will show an example of how to read an XML file using XmlDocument.

Suppose there is an XML file named employee as following :

employee.xml :


<?xml version="1.0" encoding="utf-8" ?>
<company>
    <department dep_name="HR">
        <employee>Amal</employee>
        <employee>Minnu</employee>
    </department>
    <department dep_name="IT">
        <employee>Joy</employee>
        <employee>Lia</employee>
    </department>
</company>

Below is the C# code in ASP.NET to read the employees of above xml on a
Button click :

C# Code :


we have to add the namespace using System.Xml;


protected void Button1_Click(object sender, EventArgs e)
{
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/employee.xml"));
        XmlNodeList xmlnode;
        xmlnode = xmldoc.GetElementsByTagName("department");
        for (int i = 0; i <= xmlnode.Count - 1; i++)
        {
                        string str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + "<br/>" + xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
            Label1.Text += str + "<br/>";
        }
       
}

Output :



  









Suppose there is a case that we want to save the xml file. In this case we can
use the Save method of XmlDocument as following :

C# Code :


protected void Button1_Click(object sender, EventArgs e)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/employee.xml"));
        XmlNodeList xmlnode;
        xmlnode = xmldoc.GetElementsByTagName("department");
        for (int i = 0; i <= xmlnode.Count - 1; i++)
        {
            xmlnode[0].ChildNodes.Item(0).InnerText = "Amal Antony";
            string str = xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + "<br/>" + xmlnode[i].ChildNodes.Item(1).InnerText.Trim();
            Label1.Text += str + "<br/>";
        }
        xmldoc.Save(Server.MapPath("~/employee.xml"));
    }

Resulting xml file will be :

employee.xml :


<?xml version="1.0" encoding="utf-8" ?>
<company>
    <department dep_name="HR">
        <employee>Amal Antony</employee>
        <employee>Minnu</employee>
    </department>
    <department dep_name="IT">
        <employee>Joy</employee>
        <employee>Lia</employee>
    </department>
</company>


Suppose if we want to add new node to the XML Document and save the document,
we can use CreateElement method of XmlDocument.
C# code in ASP.NET is as following :


C# Code :



protected void Button1_Click(object sender, EventArgs e)
{
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/employee.xml"));

        XmlNode node = xmldoc.DocumentElement.ChildNodes[0];

        XmlElement elem = xmldoc.CreateElement("employee");
        elem.InnerText = "Anu";

        node.AppendChild(elem);

        xmldoc.Save(Server.MapPath("~/employee.xml"));
}

The output will be as following :

employee.xml :


<?xml version="1.0" encoding="utf-8"?>
<company>
  <department dep_name="HR">
    <employee>Amal</employee>
    <employee>Minnu</employee>
    <employee>Anu</employee>
  </department>
  <department dep_name="IT">
    <employee>Joy</employee>
    <employee>Lia</employee>
  </department>
</company>

No comments:

Post a Comment