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,
- void CreateXmlFile ( String [] theArray, String filename )
- {
- StringBuilder sb = new StringBuilder (" ");
- sb.Append ("<array>");
- foreach ( String s in theArray )
- {
- sb.Append ("<elememt value = \"");
- sb.Append(s);
- sb.Append("\"/>");
- }
- sb.Append ("</array>");
- StreamWriter sw= new StreamWriter (filename);
- sw.Write (sb.ToString);
- sw.Close();
- }
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.
- public void writeAttributeString (string localName , String value )
- {
- WriteStartattribute ( null, localName, null);
- WrieString ( value );
- WriteEndAttribute ();
- }
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:
- xmlw.WriteStartElement ( "element" );
- xmlw.WriteAttributeString ( "value", s );
- xmlw.WriteAttributeString ("value", s);
- 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,
- public void WrteAttributeString {
- string prefix,
- string attr,
- string ns,
- string value);