XMLTextWriter & XMLTextReader
page 3 of 3
by Pani Baruri & Abhijit Mandrekar
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 28600/ 31
Article Contents:

XMLTextReader

The simplest way to instantiate an object of class XMLTextReader can be achieved by passing file name with path information to its constructor.

 

The XMLTextReader has following essential methods and properties.

 

Read:- This method reads each xml node from an input file. When it reaches the end of file then it returns false. When the file is read it skips attribute nodes for some reason. Therefore one should use HasAttributes property to determine whether a particular node has attributes associated with it and then browse attributes list.

 

Close:- This method closes the input stream and sets ReadState property value to Closed.

 

NodeType:- This property is one of the enumerated values from XMLNodeType enumerator that describes the type of node. The type of node can be xml element, text, comment, declaration, end element, etc.

 

EOF:- This property describes whether the end of file being read has reached. It returns true when end of file is reached.

 

HasAttributes:- This property returns true when the xml node has attributes associated with it.

 

MoveToNextAttribute:- This method causes reader to traverse to the next attribute node in attributes list of an xml element. It returns false when it reaches the end of the list.

 

IsEmptyElement:- This function returns true if the xml element has no text value. E.g. the function will return true if it encounters an element which is <middleinitial />.

 

IsStartElement:- This function returns true if the xml element is the starting tag. E.g. the function will return true if it encounters an element which is <middleinitial>.

 

ReadState:- This property is one of the enumerated values from ReadState enumerator that describes the state of the reader. While the reader is reading the file the state value is Interactive and when it reaches the end of file the state value is EndOfFile. The other values can be xml element, text, comment, declaration, end element, etc.

 

Encoding:- This property is a read-only property that describes the type of encoding present in the file.

 

WhiteSpaceHandling:- This property is one of the enumerated values from WhiteSpaceHandling enumerator that describes how to handle white spaces within the file. This property must be set to None so that white space and significant white space nodes are not generated as part of input stream. The other values can be All(generates whitespace and significant whitespace nodes) or Significant(significant whitespace nodes only).

 

The following code browses through all xml nodes in the file and lists the order in which it visits the nodes, its type, and text contents or attribute name and value or name of the node.

 

Code:

 

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ShowXML()

    End Sub

 

    Private Sub ShowXML()

        Dim xmlreader As XmlTextReader

        Dim counter As Int16 = 0

        Dim xmlString As String

        Dim vbcrlf As String = Chr(13) & Chr(10)

 

        Try

            xmlreader = New XmlTextReader(Server.MapPath(filename))    'default encoding - UTF-8

            xmlreader.WhitespaceHandling = WhitespaceHandling.None

            'While xmlreader.Read()

            xmlreader.Read()

            'While Not xmlreader.ReadState.Equals(ReadState.EndOfFile)

            While Not xmlreader.EOF

                Select Case xmlreader.NodeType

                    Case XmlNodeType.Element

                        If xmlreader.IsStartElement() Then

                            xmlString &= CStr(counter) & "-Start Element-" & xmlreader.Name & vbcrlf

                        Else

                            xmlString &= CStr(counter) & "-End Element-" & xmlreader.Name & vbcrlf

                        End If

                        If xmlreader.IsEmptyElement() Then

                            xmlString &= CStr(counter) & "-Empty Element-" & xmlreader.Name & vbcrlf

                        End If

 

                        If xmlreader.HasAttributes Then

                            counter += 1

                            While xmlreader.MoveToNextAttribute()

                                xmlString &= CStr(counter) & "-Attribute-" & xmlreader.Name & "-" & xmlreader.Value & vbcrlf

                            End While

                        End If

                    Case XmlNodeType.Text

                            xmlString &= CStr(counter) & "-Text-" & xmlreader.Value & vbcrlf

                    Case XmlNodeType.CDATA

                            xmlString &= CStr(counter) & "-CDATA-" & xmlreader.Value & vbcrlf

                    Case XmlNodeType.ProcessingInstruction

                            xmlString &= CStr(counter) & "-ProcessingInstruction-" & xmlreader.Name & "-" & xmlreader.Value & vbcrlf

                    Case XmlNodeType.Comment

                            xmlString &= CStr(counter) & "-Comment-" & xmlreader.Value & vbcrlf

                    Case XmlNodeType.XmlDeclaration

                            xmlString &= CStr(counter) & "-XmlDeclaration-" & "<?xml version='1.0'?>" & vbcrlf

                    Case XmlNodeType.Document

                    Case XmlNodeType.DocumentType

                            xmlString &= CStr(counter) & "-Document-DocumentType-" & xmlreader.Name & "-" & xmlreader.Value & vbcrlf

                    Case XmlNodeType.EntityReference

                            xmlString &= CStr(counter) & "-EntityReference-" & xmlreader.Name & vbcrlf

                     Case XmlNodeType.EndElement

                            xmlString &= CStr(counter) & "-EndElement-" & xmlreader.Name & vbcrlf

                End Select

                counter += 1

                xmlreader.Read()

                'End While

            End While

 

            ''Display the XML content to the console.

            TextBox2.ForeColor = System.Drawing.Color.Brown

            TextBox2.Text = xmlString

 

        Catch ex As Exception

            TextBox2.ForeColor = System.Drawing.Color.Red

            TextBox2.Text = ex.Message

        End Try

    End Sub


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-18 11:30:12 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search