TOC

This article is currently in the process of being translated into Dutch (~45% done).

XML:

XML schrijven met de XmlWriter class

In vorige hoofdstukken hebben we ons bezig gehouden met het lezen van XML, maar nu is het tijd om ook te gaan schrijven. Omdat XML alleen maar tekst is, zou je zomaar XML tags kunnen gaan schrijven naar een bestand en het een xml extensie geven. Maar het is gemakkelijker en veiliger om dat over te laten aan het .NET framework. Net als bij het lezen van XML, zijn er ten minste twee verschillende opties: De XmlWriter benadering en the XmlDocument benadering. Dit artikel focust op de eerste benadering, en daarna gaan we kijken naar het schrijven van XML met XmlDocument in het volgende hoofdstuk.

Het verschil tussen de twee is alweer vooral een kwestie van geheugenconsumptie. XmlWriter gebruikt minder geheugen dan XmlDocument. Maar dat is alleen maar een issue als je erg grote bestanden moet schrijven. Een ander belangrijk verschil is dat als je XmlDocument gebruikt, je een bestaand bestand kunt lezen, het manipuleren en dan de veranderingen kunt terugschrijven. Met XmlWriter moet je elke keer het hele document opnieuw vanaf nul schrijven. Dat hoeft niet noodzakelijkerwijs een probleem te zijn, maar uiteindelijk komt het aan op je eigen voorkeur.

Hier is een voorbeeld van het schrijven van XML met de XmlWriter class:

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

namespace WritingXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlWriter xmlWriter = XmlWriter.Create("test.xml");

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("users");

            xmlWriter.WriteStartElement("user");
            xmlWriter.WriteAttributeString("age", "42");
            xmlWriter.WriteString("John Doe");
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("user");
            xmlWriter.WriteAttributeString("age", "39");
            xmlWriter.WriteString("Jane Doe");

            xmlWriter.WriteEndDocument();
            xmlWriter.Close();
        }
    }
}

The above code will create the following XML:

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

So, we start by creating an instance of the XmlWriter class. It takes at least one parameter, in this case the path to where the XML should be written, but comes in many variations for various purposes. The first thing we should do is to call the WriteStartDocument() method. After that, we write a start element called "users". The XmlWriter will translate this into <users>. Before closing it, we write another start element, "user", which will then become a child of "users". We then proceed to adding an attribute (age) to the element, using the WriteAttributeString() method, and then we write the inner text of the element, by calling the WriteString() method. We then make sure to close the first "user" element with a call to the WriteEndElement() method.

This process is repeated to add another user, except that we don't call WriteEndElement() like we did before. In fact, it should be called twice, since we have an open "users" element too, but the XmlWriter will do that for us when we call the WriteEndDocument() method.

To get the XmlWriter to write our data to the disk, we call the Close() method. You can now open the file, "test.xml", in the same directory where the EXE file of your project is, usually in the bin\debug directory.

And that's really all it takes to write a simple XML file. In the next chapter, we will do the same, but using the XmlDocument class.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!