fastread homefastrread library fastread menu

C# : Writing XML with the XmlDocument class

Tutorial by:Manisha Dubey      Date: 2016-06-21 00:59:06

❰ Previous Next ❱

In the previous section, we wrote XML using the XmlWriter class. However, for some situations, especially when doing updates of existing XML, using the XmlDocument class can come in handy. You should be aware of the higher memory consumption though, mainly for large XML documents. Here is some code:

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode rootNode = xmlDoc.CreateElement("users");
            xmlDoc.AppendChild(rootNode);

            XmlNode userNode = xmlDoc.CreateElement("user");
            XmlAttribute attribute = xmlDoc.CreateAttribute("age");
            attribute.Value = "42";
            userNode.Attributes.Append(attribute);
            userNode.InnerText = "John Doe";
            rootNode.AppendChild(userNode);

            userNode = xmlDoc.CreateElement("user");
            attribute = xmlDoc.CreateAttribute("age");
            attribute.Value = "39";
            userNode.Attributes.Append(attribute);
            userNode.InnerText = "Jane Doe";
            rootNode.AppendChild(userNode);

            xmlDoc.Save("test-doc.xml");
        }
    }
}

And the resulting XML:

<users>
  <user age="42">John Doe</user>
  <user age="39">Jane Doe</user>
</users>

As you can see, this is a bit more object oriented than the XmlWriter approach, and it does require a bit more code. What we do is that we instantiate an XmlDocument object, which we will use to create both new elements and attributes, using the CreateElement() and CreateAttribute() methods. Each time we do that, we append the elements, either directly to the document, or to another element. You can see this in the example, where the root element ("users") is appended directly to the document, while the user elements are appended to the root element. Attributes are of course appended to the elements where they belong, using the Append() method on the Attributes property. The entire XML document is written to the disk on the last line, where we use the Save() method.

Now, as already mentioned, this requires a bit more code than when using the XmlWriter, but imagine a situation where you just need to go into an existing XML document and change a few values. Using the XmlWriter approach, you would have to first read all the information using an XmlReader, store it, change it, and then write the entire information back using the XmlWriter. Because the XmlDocument holds everything in memory for you, updating an existing XML file becomes a lot simpler. The following example opens the "test-doc.xml" file that we created in the previous chapter and increases every user's age by one. See how easy it is:

using System;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("test-doc.xml");
            XmlNodeList userNodes = xmlDoc.SelectNodes("//users/user");
            foreach(XmlNode userNode in userNodes)
            {
                int age = int.Parse(userNode.Attributes["age"].Value);
                userNode.Attributes["age"].Value = (age + 1).ToString();
            }
            xmlDoc.Save("test-doc.xml");           
        }
    }
}

We load the XML file and ask for all the <user> nodes. We then iterate through them, read the age attribute into an integer variable, and then we write the value back to the node and attribute, after increasing the value by 1. At last, we save the document back to the same file and if you open it up, you will see that our users all just had a birthday. Pretty cool!

❰ Previous Next ❱


C#

Submit Your Thought, Tutorial, Articls etc.

Submit Your Information India's Number one online promotion website