[C# – XML] Writing XML documents

Using LINQ, you can write a XML document quite intuitively. But this article will focus on the legacy classes in the “System.Xml” namesapce.

 

1. Writing XML Files

System.Xml.XmlWriter” is an abstract class.

  • Use the XmlWriter’s derived classes to write XML
  • Provides fast, non-cashed forward only access to XML data

 

2.XmlTextWriter

System.Xml.XmlTextWriter” provides forward-only access to a stream of XML data.

(1) Constructors

Constructors of “XmlTextWriter” takes the combination of following parameters:

  • Stream w: the stream to which you want to write
  • string filename: the filename to write to
  • TextWriter w:
  • Encoding encoding: the encoding to generate (the default is UTF-8)

The recommended way to create an “XmlWriter” instance is to use the static “Create()” methods using “XmlWriterSettings“.

(2) Methods

// Documents
void WriteStartDocument() // writes the XML declaration with the version "1.0"
void WriteStartDocument(bool standalone) // writes the XML declaration with the version "1.0" and the standalone attribute
// Elements: writes the specified start/end tag.
void WriteStartElement(string name)
void WriteEndElement()
// Attributes
void WriteAttributeString(string name, string value)
// Writes the given text content.
void WriteString(string text)

 

3. XmlWriterSettings

You can set the following properties:

public Encoding Encoding { get; set; } // the default is Encoding.UTF8
public bool Indent { get; set; } // the default is false
public string IndentChars { get; set; } // the default is two spaces
public bool OmitXmlDeclaration { get; set; } // the default is false, an XML declaration is written
public bool NewLineOnAttributes { get; set; } // the default is false.

 

4. Using XmlTextWriter

  • Create an “XmlWriter” object using the static “Create()” method with the “XmlWriterSettings” object
  • Write the document using the methods of the “XmlWriter” object

 

5. Sample code

We’ll make a program to write the following short XML document.

<?xml version="1.0" encoding="utf-8"?>
<book isbn="1551925702">
  <title>Harry Potter and the Order of the Phoenix</title>
  <author>J.K. Rowling</author>
</book>

 

public static void writeXML()
{
  string fileStr = @"c:csharpxmlbook2.xml";
  XmlWriter writer = null;
  try
  {
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;

    writer = XmlWriter.Create(fileStr, settings);
    writer.WriteStartDocument();
    writer.WriteStartElement("book");
    writer.WriteAttributeString("isbn", "1551925702");
    writer.WriteStartElement("title");
    writer.WriteString("Harry Potter and the Order of the Phoenix");
    writer.WriteEndElement();
    writer.WriteElementString("author", "J.K. Rowling");
    writer.WriteEndElement();
  }
  catch (XmlException e)
  {
    System.Console.WriteLine(e.Message);
    System.Console.WriteLine(e.StackTrace);
  }
  finally
  {
    if (writer != null)
    {
      writer.Close();
    }
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s