Description





Description

When a writer is instantiated, the write state is set to Start. While content is written, the write state is set to reflect the type of content being written. When the XmlWriter.Close method is called, the write state is set to Closed. The XmlWriter.WriteEndDocument method resets the write state back to Start, allowing the writer to write a new XML document.

Example

using System;

using System.Xml;



/// <summary>

/// Writes the WriteState at each point in the writing of an .xml input file

/// </summary>



public class WriteStateSample

{

    public static void Main()

    {

        XmlTextWriter w = new XmlTextWriter("sample.xml", null);

        ShowWriteState("constructor", w);

        w.WriteStartDocument();

        ShowWriteState("WriteStartDocument", w);

        w.WriteStartElement("root");

        ShowWriteState("WriteStartElement", w);

        w.WriteStartAttribute("sample", "");

        ShowWriteState("WriteStartAttribute", w);

        w.WriteString("value");

        ShowWriteState("WriteString", w);

        w.WriteEndAttribute();

        ShowWriteState("WriteEndAttribute", w);

        w.WriteElementString("subelement", "sample");

        ShowWriteState("WriteElementString", w);

        w.WriteEndElement();

        ShowWriteState("WriteEndElement", w);

        w.WriteEndDocument();

        ShowWriteState("WriteEndDocument", w);

        w.Close();

        ShowWriteState("Close", w);

        Console.WriteLine();

        Console.WriteLine();

        Console.WriteLine("Press Enter to continue");

        Console.ReadLine();

    }



    static void ShowWriteState(string s, XmlTextWriter w)

    {

        Console.WriteLine("Called method {0}, "

            + "WriteState is {1}",

            s, w.WriteState);

    }



}


The output is


Called method constructor, WriteState is Start

Called method WriteStartDocument, WriteState is Prolog

Called method WriteStartElement, WriteState is Element

Called method WriteStartAttribute, WriteState is Attribute

Called method WriteString, WriteState is Attribute

Called method WriteEndAttribute, WriteState is Element

Called method WriteElementString, WriteState is Content

Called method WriteEndElement, WriteState is Content

Called method WriteEndDocument, WriteState is Start

Called method Close, WriteState is Closed





Press Enter to continue



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows