XmlWriter

XmlWriter is a forward-only writer of an XML stream. The XmlWriter is symmetrical to XmlReader.

You construct an XmlWriter by calling Create with an optional settings object.

In the following example, we enable indenting to make the output more human-readable, and then write a simple XML file:


using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Text;
using System.IO;
class Program
{
    static void Main()
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;

        using (XmlWriter writer = XmlWriter.Create("foo.xml", settings))
        {
            writer.WriteStartElement("customer"); 
            writer.WriteElementString("firstname", "Jack"); 
            writer.WriteElementString("lastname", "Smith"); 
            writer.WriteEndElement();
        }

    }
}

The content of foo.xml

 
      <?xml version="1.0" encoding="utf-8"?>
      <customer>
        <firstname>Jack</firstname>
        <lastname>Smith</lastname>
      </customer>
  

XmlWriter automatically escapes characters that would otherwise be illegal within an attribute or element, such as &amp; &lt; &gt;, and extended Unicode characters.

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.