While developing ASP applications the developers used XML tag to reference XML files on the server or to refer to XML data islands within HTML file or they developed a complex code to concatenate xml data with xml tags. The inconvenience of having ASP code to generate xml output was cumbersome to maintain. Also the code was parsing for unacceptable xml characters such as “,”, “;”, “&”, “<”, “>” and converting them to &, <,, > characters respectively.
The .net platform has overcome the above mentioned problems by defining a class for writing xml output to a file using abstract classes XMLTextReader and XMLTextWriter. The XMLTextWriter is a sequential text file writer and its counterpart is XMLTextReader class which is sequential text file reader. The order of reading has to be essentially same as order of writing. XmlReader provides a fast, forward-only, read-only cursor for processing an XML document stream. XmlWriter provides an interface for producing XML document streams that conform to the W3C's XML 1.0 + Namespaces Recommendations. Both classes imply a streaming model that doesn't require an expensive in-memory cache. The methods of the class should be used in a specific order so that it generates a well formed and valid xml file.
The XmlTextWriter has different constructors to specify the location to write the XML data to. To instantiate an object of class XMLTextWriter, xml file name and encoding type parameters are passed. If encoding parameter is set to Nothing then default encoding will be <A href=’ http://www.faqs.org/rfcs/rfc2044.html”>UTF-8</A>. The other encodings supported by this class is ASCII and Unicode. The encoding values can be obtained from System.Text.Encoding class.
The XMLTextWriter has following essential methods.
WriteStartDocument:- The very first method after an instantiation of XMLTextWriter object to open new xml document with xml declaration with version 1.0.
WriteEndDocument:- The very last method after writing xml output to the file to close xml document.
WriteStartElement:- This method adds an xml tag to the contents. These tags generally denote themselves as parent tags. It accepts tag name as an input.
WriteEndElement:- This method closes the recently opened xml tag. If xml tags are nested then it closes the innermost tag first.
WriteElementString:- This method adds sibling tags as name and value pairs.
WriteStartAttribute:- This method appends attributes to the recently added xml tag. It accepts attribute name, xml namespace and prefix string to be used before each xml tag as an input to the method. The xml name space and prefix string can be blank strings.
WriteString:- This method must follow WriteStartAttribute method to associate value of an attribute with xml tag.
WriteEndAttribute:- This method closes the recently added attribute.
Flush:- This method actually writes buffered xml output obtained from methods described above to the physical file.
WriteComments:- Use this method to write meaningful comment any time during xml output generation. It accepts comment string as an input.
Formatting:- This is a property by which the xml output is automatically indented as you write xml tags and its contents. This way if the file is opened in any text reader it will not look like a long string.