Skip to content


Write XML file using XmlTextWriter

I will give an example on how to create a simple XML-file by using the XmlTextWriter. It’s actually quite simple, and not at all as time consuming as I first thought when I started playing around with it.

XML namespace
First of all we have to add the namespace for XML:

Using System.XML;

Begin the document
The document is created using the next lines of code, and Encoding is also chosen (Choose UTF8 usually, but in some cases ANSI encoding is wanted).

  
//XmlTextWriter writer = new XmlTextWriter(Console.Out);
XmlTextWriter writer = new XmlTextWriter("XmlTextWriterTest.xml", Encoding.UTF8);
 
writer.WriteStartDocument();
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;

Notice that when you make a console program, you can output the xml directly to the screen, which is what I use when I am just quickly testing out some code that spits out xml.

WriteStartDocument() writes the

I also always use indentation, and I have used an indentation value of 2 spaces. This makes it look nice.

Add the data to the XML file

 writer.WriteComment("This is demonstration in how to create a simple xml-file, using XmlTextWriter");
 
 writer.WriteStartElement("Employees");
 writer.WriteStartElement("Employee");
 
 writer.WriteAttributeString("Id", "436");
 
 writer.WriteStartElement("Name");
 writer.WriteAttributeString("Firstname", "Thomas");
 writer.WriteAttributeString("Middlename", "N/A");
 writer.WriteAttributeString("Lastname", "Lund");
 writer.WriteEndElement(); //End Name
 
 writer.WriteStartElement("Personal");
 writer.WriteElementString("Age", "31");
 
 writer.WriteStartElement("Family");
 writer.WriteStartElement("Partner");
 writer.WriteAttributeString("Married", "Yes");
 writer.WriteString("Mia Lund");
 writer.WriteEndElement(); //End Partner
 
 writer.WriteStartElement("Children");
 writer.WriteAttributeString("Amount", "1");
 writer.WriteElementString("Name", "Noah Lund");
 writer.WriteEndElement(); //End Children
 
 writer.WriteEndElement(); //End Family
 writer.WriteEndElement(); //End Personal
 
 writer.WriteElementString("Salary", "Works for peanuts");
 
 writer.WriteEndElement(); //Ends Employee
 writer.WriteEndElement(); //Ends Employees
 writer.WriteEndDocument();
 writer.close();

Comment is pretty selfexplanatory. All it does it comment out a piece of text in the xml-file
writer.WriteStartElement(“Employees”); writes and WriteEndElement() closes the last opened Element.

You can nest as many WriteStartElements as you wish, and you can also throw in a few WriteAttributesString(“string”, “Value”) to write attributes to the last opened Element.

The above code writes the following XML:

<?xml version="1.0" encoding="utf-8"?>
<!--This is demonstration in how to create a simple xml-file, using XmlTextWriter-->
<Employees>
  <Employee Id="436">
    <Name Firstname="Thomas" Middlename="N/A" Lastname="Lund" />
    <Personal>
      <Age>31</Age>
      <Family>
        <Partner Married="Yes">Mia Lund</Partner>
        <Children Amount="1">
          <Name>Noah Lund</Name>
        </Children>
      </Family>
    </Personal>
    <Salary>Works for peanuts</Salary>
  </Employee>
</Employees>

That’s it for how to make a simple xml-file. Have fun with it!

DeliciousDiggTwitterFacebookLinkedInTechNetStumbleUponNewsVineTechnorati FavoritesSlashdotMSDNShare

No related posts.

Posted in C#.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin