Home » Asp.Net » XmlDocument class

XmlDocument class

In the previous article we have seen how to write an XML file using XmlWriter class, in this article we will see how to write an XML file using XmlDocument class. Both are writing the XML file only, however when we want to update an existing XML file then XmlDocument class will be easy to use. Because XmlWriter class will use higher memory consumption.

Now look at the below sample code


using System.Xml;

namespace DocumentXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument document = new XmlDocument();
            XmlNode rootElement = document.CreateElement("developers");
            document.AppendChild(rootElement);

            XmlNode developerNode = document.CreateElement("developer");
            XmlAttribute noteAttribute = document.CreateAttribute("dept");
            noteAttribute.Value = "DotNet";
            developerNode.Attributes.Append(noteAttribute);
            developerNode.InnerText = "Tarun Dudhatra";
            rootElement.AppendChild(developerNode);

            developerNode = document.CreateElement("developer");
            noteAttribute = document.CreateAttribute("dept");
            noteAttribute.Value = "DotNet";
            developerNode.Attributes.Append(noteAttribute);
            developerNode.InnerText = "Milan Dudhatra";
            rootElement.AppendChild(developerNode);

            document.Save(@"D:\Tarun\developers.xml"); // Change file path here
        }
    }
}

Above code will produce below output


<developers>
  <developer dept="DotNet">Tarun Dudhatra</developer>
  <developer dept="DotNet">Milan Dudhatra</developer>
</developers>

As we can see, first we have created an object of XmlDocument class. It will be used to create new element and new attribute both using the CreateElement() and CreateAttribute() methods. Each time we will do the same process, what I mean here is if we want to create 3 nodes in the root element developers then we will do use document object 3 times to create XmlNode element using the CreateElement(). We append the element to either root element or any other element using .Append() method of the Attributes property. Attributes are appended to the element itself where we want to add in any node. Once all the nodes are created with attributes and appended to the rool element or any other element, the entire XML file is written to the disk using Save() method, which we have write at the last line.

It’s Pretty cool, Isn’t it?

Now download the sample project with code written earlier.

About

Site Admin is a writer of http://www.codingsharp.com website. He is a software professional with hands on 4+ years of experience. Generally he writes an article about Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.

About Coding Sharp

Coding sharp is a blog where you can find the authors personal experience, tutorials, examples, code samples, resources related to C#, Asp.Net, Linq, Ajax, MVC, Entity Framework, SQL Server, jQuery, Visual Studio.

Pages