XML Writers

Introduction

 
XML documents are text files, which consist of markup text. Create an XML document programmatically by putting a string of text after another. To write XML code, the Microsoft .NET framework is used for more refinement and elegance.
 

XML Writer Programming Interface

 
XML Writer represents a component which forward otput xml data to streams. XML writer guarantees by designing all xml data. To render the content of a string array in XML, the below code is used to fits the bill,
  1. void CreateXmlFile  ( String [] theArray, String filename )    
  2. {    
  3.  StringBuilder sb = new StringBuilder (" ");    
  4.  sb.Append ("<array>");    
  5.  foreach ( String s in theArray )    
  6. {    
  7.  sb.Append ("<elememt value = \"");    
  8.  sb.Append(s);    
  9.  sb.Append("\"/>");    
  10. }    
  11. sb.Append ("</array>");    
  12. StreamWriter sw= new StreamWriter (filename);    
  13. sw.Write (sb.ToString);    
  14. sw.Close();    
  15. }     
XML writer is a specialized class used to write xml data to variety of storage media. It features ad hoc method to write any special item. 
 

XmlWriter Base Class

 
The XML writer class is not directly created from user application and it is used as a reference type of object that instance of a class derived from XmlWriter.     
  • WriteState - Read-only property that gets the state of the writer and it can have any value taken from WriteState enumeration.
  • XmlLang - Read-only property that return the current XML land scope and sets the language of the document by writing an XML lang attribute to the output stream.
  • XmlSpace - Read-only property that indicates the current XML space scope through a value taken from XmlSpace enumeration. 

XML Writer States

 
The state is set to start when creating a writer and the next state is prolog which occurred during the calling of WriteStartDocument. The state is converted to attribute during calling the WriteStart Attribute method. The state return starts unless closing the writer.
  • Attribute: The writer enters the state when the attribute is written.
  • Element: The writer enters the state when the start tag is written.
  • Content: The writer enters the state when the content of the node is written.
  • Start: The writer is in the initial state waiting for a write call to be issued.
WriteAttributeString is implemented in XMlWriter like the below code and the XML writer class provides an implementation of one shot method that groups basic calls.
  1. public void writeAttributeString (string localName , String value )    
  2. {    
  3.  WriteStartattribute ( null, localName, null);    
  4.  WrieString ( value );    
  5.  WriteEndAttribute ();    
  6. }    

XML text writer class

 
XML Writer is an abstract class and some methods have concrete implementation. It provide  standard implementation for all method and properties and it maintain internal stack to keep track of xml elements opened. It has three constructors and the constructor allow to build an xml text writer starting from textWriter object. The below code is used to demonstrate to wrte two identical attribute for specific nodes:
  1. xmlw.WriteStartElement ( "element" );    
  2. xmlw.WriteAttributeString ( "value", s );    
  3. xmlw.WriteAttributeString ("value", s);    
  4. xmlw.WriteEndElement ();     

Building an XML Document

  • Initialize The document
    The outputstream is open and the stage is simplified to write the xml prolog, including xml declaration and heading information.

  • write data
    Create an XML node such as element node attribute and parsable text entities, white spaces. The writer maintains an internal node stack and uses to detect and block calls and created outside start tag.

  • Closing document
    Close the writer to flush both contents of the writer and stream object. The XML text accumulated in the internal buffer is written.

Namespace Declaration

 
The namespace declaration in the current node using xmlns attribute. The prefix is a symbolic name that has a unique identifier in the namespace. To declarer the namespace, it needs to add a special attribute to the node that roots the targe scope of namespace. The writeAttributestring method is used to overload as shown below,
  1. public void WrteAttributeString {  
  2.     string prefix,  
  3.     string attr,  
  4.     string ns,  
  5.     string value);   


Similar Articles