Create XML with XmlWriter in CSharp

Description

The following code shows how to create XML with XmlWriter.

Example


//  w w w  .j  a v  a2  s. c om
using System;
using System.IO;
using System.Xml;
   

class MainClass
{
    static public void Main()
    {
        XmlTextWriter XmlWriter;
    

        XmlWriter = new XmlTextWriter(Console.Out);
        
        XmlWriter.WriteStartDocument();
        XmlWriter.WriteComment("This is a XML document");
        XmlWriter.WriteStartElement("BOOK");
        XmlWriter.WriteElementString("TITLE", "C#");
        XmlWriter.WriteElementString("AUTHOR", "java2s.com");
        XmlWriter.WriteElementString("PUBLISHER", "java2s.com");
        XmlWriter.WriteEndElement();
        XmlWriter.WriteEndDocument();
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style