XMLTextWriter & XMLTextReader
page 1 of 3
Published: 03 Dec 2003
Unedited - Community Contributed
Abstract
This article describes how to write pre-defined XML to a file by using XmlTextWriter class. XmlTextWriter provides a fast, forward-only way of generating XML documents without the overhead of a DOM. …
by Pani Baruri & Abhijit Mandrekar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28597/ 31
Article Contents:

XML TextWriter

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 &amp, &lt,, &gt 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.

 


View Entire Article

User Comments

Title: How to add a record to xml file?   
Name: Tarak
Date: 2011-04-08 12:50:18 PM
Comment:
Hi,
I found that the data in xml file is overriding when i use XmlTextWriter. Can u tell how to add a record to xml file?
Title: Very Good   
Name: Mitu Nayak
Date: 2011-01-03 3:52:28 AM
Comment:
Very Good XML Starter..........
Title: Very Good About Xml Starter   
Name: Rahul Dhawan
Date: 2010-01-22 2:48:25 AM
Comment:
Very Good About Xml Starter. it provides a very useful info about XMl learner. Great
Title: good but if it is in c#,....   
Name: venkat
Date: 2009-10-08 8:03:15 AM
Comment:
very good abt writer
Title: Xml   
Name: Raja Subramanian
Date: 2009-09-14 2:40:18 AM
Comment:
Excellent article, please provide more article like this.
Done good programming by using this article and this article gave me good knowledge for me about xml.
Title: Awesome   
Name: Charles
Date: 2009-08-26 4:20:52 PM
Comment:
Thank you!
Title: hotcoder   
Name: Muhammad Daud
Date: 2008-11-06 1:22:36 AM
Comment:
Thanks for such a helpful tutorial
Title: Code gave me more knowledge on XML TextWriter   
Name: Durga Prasad
Date: 2008-02-28 4:04:34 AM
Comment:
Excellent article, please provide more article like this.
Done good programming by using this article
Title: Solve the problem   
Name: ram
Date: 2008-01-25 2:12:20 AM
Comment:
\
Title: xml   
Name: KAUSHAL KEDIA
Date: 2008-01-05 4:19:36 AM
Comment:
ITS GOOD ..............
Title: Mathematics   
Name: Durai
Date: 2007-11-08 9:32:04 PM
Comment:
Very Good
Title: Very helpful!   
Name: Karen Timby
Date: 2007-11-06 5:32:39 PM
Comment:
Thanks for the easy to understand summary!
Title: please help   
Name: liju84@gmail.com
Date: 2007-09-04 3:50:28 AM
Comment:
xmlString &= CStr(counter) & "-EndElement-" & xmlreader.Name & vbcrlf


can i get the c# version of the above statement
thx
Title: Nice Article   
Name: Gaurav
Date: 2007-06-01 1:50:49 AM
Comment:
Nice Article Guys!
Title: Software Developer   
Name: Afroz Shaikh
Date: 2006-11-17 4:11:15 PM
Comment:
Excellent Code
Title: I'm making a music box   
Name: Nguyen Minh Son
Date: 2006-09-14 7:58:24 AM
Comment:
Hello It good for me
Title: Very Helpful   
Name: Vishnuprem
Date: 2006-07-19 12:32:04 AM
Comment:
This article is very helpful to us.

- Thanks
Title: Mr.   
Name: Raj
Date: 2006-05-23 10:52:24 AM
Comment:
Very Helpful... G8! ..Keep going ....
Title: Good article!   
Name: cool_guy
Date: 2005-10-03 5:57:16 AM
Comment:
The article is good.I liked it.
Title: Excellent Code   
Name: Hasham Ahmed Qazi
Date: 2005-07-27 8:27:54 AM
Comment:
thanx this works fine..very good example to write XML files.even for multiple attributes.
Title: Multiple Attributes   
Name: Rohan Chandane
Date: 2005-04-13 2:54:00 AM
Comment:
How to add multiple attributes?
This is adding only one Attribules [using ASP.net(c#)] -
-------------------------------------
writer.WriteStartElement("Category");
writer.WriteStartAttribute("Name","");
writer.WriteName("Lights");
writer.WriteEndAttribute();
-------------------------------------

Thanks
Title: Dhr   
Name: DoJa
Date: 2004-06-16 8:23:59 AM
Comment:
Great!

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 9:33:21 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search