Illustrates the XmlTextWriter class : XML Write « XML « C# / C Sharp






Illustrates the XmlTextWriter class

 
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/


/*
  Example20_1.cs illustrates the XmlTextWriter class
*/

using System;
using System.Xml;

public class Example20_1 
{

    public static void Main() 
    {

        // use the XmlTextWriter to open a new XML file
        XmlTextWriter xw = new XmlTextWriter(@"Cust4.xml", 
            System.Text.Encoding.UTF8);

        // write the document declaration
        xw.WriteStartDocument();

        // write the first element
        xw.WriteStartElement("NewDataSet");

        // write the first customer
        xw.WriteStartElement("Customers");
        xw.WriteElementString("CustomerID", "ALFKI");
        xw.WriteElementString("CompanyName", "Alfreds Futterkiste");
        xw.WriteElementString("ContactName", "Maria Anders");
        xw.WriteElementString("ContactTitle", "Sales Representative");
        xw.WriteElementString("Address", "Obere Str. 57");
        xw.WriteElementString("City", "Berlin");
        xw.WriteElementString("PostalCode", "12209");
        xw.WriteElementString("Country", "Germany");
        xw.WriteElementString("Phone", "030-0074321");
        xw.WriteElementString("Fax", "030-0076545");
        xw.WriteEndElement();

        // write the second customer
        xw.WriteStartElement("Customers");
        xw.WriteElementString("CustomerID", "BONAPP");
        xw.WriteElementString("CompanyName", "Bon App'");
        xw.WriteElementString("ContactName", "Laurence Lebihan");
        xw.WriteElementString("ContactTitle", "Owner");
        xw.WriteElementString("Address", "12, rue des Bouchers");
        xw.WriteElementString("City", "Marseille");
        xw.WriteElementString("PostalCode", "13008");
        xw.WriteElementString("Country", "France");
        xw.WriteElementString("Phone", "91.24.45.40");
        xw.WriteElementString("Fax", "91.24.45.41");
        xw.WriteEndElement();

        // end the NewDataSet element
        xw.WriteEndElement();

        // end the document
        xw.WriteEndDocument();

        // flush and close
        xw.Flush();
        xw.Close();
    }

}

/**/

           
         
  








Related examples in the same category

1.Create XML document with XmlWriter
2.Read and Write XML Without Loading an Entire Document into MemoryRead and Write XML Without Loading an Entire Document into Memory
3.Writing XML with the XmlWriter ClassWriting XML with the XmlWriter Class
4.Write To XML File
5.Write XML document to console
6.XML Write: comment, start element, end element, attribute
7.Create XmlWriter using the static XmlWriter.Create method.