ADO.NET Reading XML in VB.NET

In this article I will explain you how to read a XML file in ADO.NET.
  • 3096

The XmlReader is an abstract base class for XML reader classes. This class provides fast, non-cached forward-only cursors to read XML documents.

The XmlTextReader, XmlNodeReader, and XmlValidatingReader classes are defined from the XmlReader class. Figure 6-6 shows XmlReader and its derived classes.

Figure-6.6.gif

Figure 6-6. XmlReader classes

You use the XmlTextReader, XmlNodeReader, and XmlValidatingReader classes to read XML documents. These classes define overloaded constructors to read XML files, strings, streams, TextReader objects, XmlNameTable, and combinations of these. After creating an instance, you simply call the Read method of the class to read the document. The Read method starts reading the document from the root node and continues until Read returns false, which indicates there is no node left to read in the document. Listing 6-9 reads an XML file and displays some information about the file. In this example I'll use the books.xml file. You can use any XML by replacing the string name.

Listing 6-9. Reading an XML file


        Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio" & vbCr & vbLf & "2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")
        Console.WriteLine("General Information")
        Console.WriteLine("= = = = = = = = = ")
        Console.WriteLine(reader.Name)
        Console.WriteLine(reader.BaseURI)
        Console.WriteLine(reader.LocalName)


Getting Node Information

The Name Property returns the name of the node with the namespace prefix, and the LocalName property returns the name of the node without the prefix.

The Item is the indexer. The Value property returns the value of a current node. you can even get the level of the node by using the Depth property, as shown in this example:


Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")

        While reader.Read()
            If reader.HasValue Then
                Console.WriteLine("Name : " & reader.Name)
                Console.WriteLine("Node Depth: " & reader.Depth.ToString())
                Console.WriteLine("Value : " & reader.Value)
            End If
        End While

The Node Type property returns the type of the current node in the form of XmlNodeType enumeration:


        Dim type As XmlNodeType = reader.NodeType

Which defines the type of a node. The XmlNodeType enumeration members are Attribute, CDATA, Comment, Document, Element, WhiteSpace, and so on. These represent XML document node types.

In Listing 6-10, you read a document's nodes one by one and count them. Once reading and counting are done, you see how many comments, processing instructions, CDATAs, elements, whitespaces, and so on that a document has and display them on the console. The XmlReader.NodeType property returns the type of node in the form of XmlNodeType enumeration. The XmlNodeType enumeration contains a member corresponding to each node types. You can compare the return value with XmlNode Type members to find out the type of a node.

Listing 6-10. Getting node information


Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Data.Common
Imports System.Xml
Imports System.IO

Namespace ConsoleApplication2
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim DecCounter As Integer = 0, PICounter As Integer = 0, DocCounter As Integer = 0, CommentCounter As Integer = 0
            Dim ElementCounter As Integer = 0, AttributeCounter As Integer = 0, TextCounter As Integer = 0, WhitespaceCounter As Integer = 0
            Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")

            While reader.Read()
                Dim nodetype As XmlNodeType = reader.NodeType
                Select Case nodetype
                    Case XmlNodeType.XmlDeclaration
                        DecCounter += 1
                        Exit Select
                    Case XmlNodeType.ProcessingInstruction
                        PICounter += 1
                        Exit Select
                    Case XmlNodeType.DocumentType
                        DocCounter += 1
                        Exit Select
                    Case XmlNodeType.Comment
                        CommentCounter += 1
                        Exit Select
                    Case XmlNodeType.Element
                        ElementCounter += 1
                        If reader.HasAttributes Then
                            AttributeCounter += reader.AttributeCount
                        End If
                        Exit Select
                    Case XmlNodeType.Text
                        TextCounter += 1
                        Exit Select
                    Case XmlNodeType.Whitespace
                        WhitespaceCounter += 1
                        Exit Select
                End Select
            End While

            ' print the info
            Console.WriteLine("White Spaces:" & WhitespaceCounter.ToString())
            Console.WriteLine("Process Instruction:" & PICounter.ToString())
            Console.WriteLine("Declaration:" & DecCounter.ToString())
            Console.WriteLine("White Spaces:" & DocCounter.ToString())
            Console.WriteLine("Comments:" & CommentCounter.ToString())
            Console.WriteLine("Attributes:" & AttributeCounter.ToString())
            Console.ReadLine()
        End Sub
    End Class
End Namespace

Output of above coding

readXMLOutput1.gif

The case statement can have values XmlNodeType.XmlDeclaration, XmlNodeType.ProcessingInstruction, XmlNodeType.DocumentType, XmlNodeType.Comment, XmlNodeType.Element, XmlNodeType.Text, XmlNodeType.Whitespace, and so on.

The XmlNodeType enumeration specifies the type of node. Table 6-4 describes its members.

Table 6-4. the xml Node Type Enumeration's members
 

MEMBER NAME

DESCRIPTION

Attribute

Attribute node

CDATA

CDATA section

Comment 

Comment node

Document

Document object

DocumentFragment

Document Fragment

DocumentType

The DTD, indicated by the <! DOCTYPE> tag

Element

Element node

EndElement

End of element

EndEntity

End of an entity

Entity

Entity declaration

EntityReference

Reference to an entity

None

Returned if XmlReader is not called yet

Notation

Returned if XmlReader is not called yet

ProcessingInstruction

Represents a processing instruction (PI) node

SignificationWhitespace

Represents white space between markup in a mixed content model

Text

Represent the text content of an element

Whitespace

Represents white space between markup

XmlDeclaration

Represents an XML declaration nod

Moving to a Content

You can use the MoveToMethod to move from the current to the next content node of an XML document. A content's node is an item of the following type: text CDATA, Element, EntityReference, or Entity. So if you call the MoveToContent method, it skips other types of nodes besides the content type nodes. For example if the next node of the current node is DxlDeclaration, or DocumentType, it will skip these nodes until it finds a content type node. See the following example:


        Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio" & vbCr & vbLf & "2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")
        If reader.Read() Then
            Console.WriteLine(reader.Name)
            reader.MoveToContent()
            Console.WriteLine(reader.Name)
        End If


The Get Attributes of a Node

The GetAttribute method is an overloaded method. You can use this method to return attributes with the specified name, index, local name, or namespace URI. You use the HasAttributes property to check if a node has attributes, and AttributesCount returns the number of attributes on the node. The local name is the name of the current node without prefixes. For example, if <bk:book> represents a name of a node, where bk is a namespace and: is used to refer to the namespace, the local name for the <bk:book> element is book. MoveToFirstAttributes moves to the first attribute. The MoveToElement method moves to the element that contains the current attributes node (see listing 6-11).

Listing 6-11. Get Attributes of a node


Imports System
Imports System.Xml

Class XmlReaderSamp
    Private Shared Sub Main(ByVal args As String())
        Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio" & vbCr & vbLf & "2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")        reader.MoveToContent()
        reader.MoveToFirstAttribute()
        Console.WriteLine("First Attribute value" & reader.Value)
        Console.WriteLine("First Attribute Name" & reader.Name)
 
        While reader.Read()
            If reader.HasAttributes Then
                Console.WriteLine(reader.Name & "Attribute")
                For i As Integer = 0 To reader.AttributeCount - 1
                    reader.MoveToAttribute(i)
                    Console.WriteLine(("Nam: " & reader.Name & ", value: ") + reader.Value)
                Next

                reader.MoveToElement()
            End If
        End While
        Console.ReadLine()
    End Sub
End Class

Output of above coding

readXMLOutput3.gif

You can move to attributes by using MoveToAttribute, MoveToFirstAttribute, and MoveToNextAttribute. MoveToFirstAttribute and MoveToNextAttribute move to the first and next attributes, respectively. After calling MoveToAttribute, the Name, Namespace, and Prefix property will reflect the properties of the specified attribute.

Searching for a Node

The Skip method skips the current node. It's useful when you're looking for a particular node and want to skip other nodes. In listing 6-12, you read your books.xml document and compare its XmlReader.Name(through XmlTextReader) to look for a node with name bookstore and display the name, level, and value of that node using XmlReader's Name, Depth, and Value properties.

Listing 6-12. Skip Method


        Dim reader As New XmlTextReader("C:\Documents and Settings\PuranMAC\My Documents\Visual Studio 2008\Projects\ConsoleApplication2\ConsoleApplication2\XMLFile1.xml")

        While reader.Read()
            ' Look for a Node with name bookstore
            If reader.Name <> "bookstore" Then
                reader.Skip()
            Else
                Console.WriteLine("Name: " & reader.Name)
                Console.WriteLine("Level of the node:" & reader.Depth.ToString())
                Console.WriteLine("Value: " & reader.Value)
            End If
        End While

Closing the Document

Finally, use Close to close the opened XML document.

Table 6-5 and 6-6 list the XmlReader class properties and methods. I've discussed some of them already.

Table 6-5 xml Reader properties

 

PUBLIC INSTANCE PROPERTY

DESCRIPTION

AttributeCount

Returns the number of attributes on the current node

BaseURI

Returns the base URI of the current node

Depth

Returns the level of the current node

EOF

Indicates whether its pointer is at the end of the stream

HasAttributes

Indicates if a node has attributes or not

HasValue

Indicates if a node has a value or not

IsDefault

Indicates whether the current node is an attributes generated from the default value defined in the DTD or schema

IsEmptyTag

Returns if the current node is empty or not

Item

Returns if value of the attribute

LocalName

Name of the current node without the namespace prefix

Name

Name of the current node with the namespaces prefix

NamespaceURI

Namespace uniform Resource Name (URN) of the current namespace scope

NameTable

Returns the XmlNameTable associated with this implementation

NodeType

Returns the type of node

Prefix

Returns the namespace associated with a node

ReadState

Read state

Value

Returns the value of a node

XmlLang

Returns the current xml:lang scope

XmlSpace

Returns the current xml:space scope

Table 6-6. xml Reader Methods

 

PUBLIC INSTANCE METHOD

DESCRIPTION

Close

Close the stream and changes ReadState to Closed

GetAttribute

Returns the value of an attribute

IsStartElement

Checks if a node has start tag

LookupNamespace

Resolves a namespace prefix in the current element's scope

MoveToAttribute, MoveToContent, MoveToElement,

Moves to specified attributes, content, and element

MoveToFirstAttribute, MoveToNextAttribute

Moves to the first and next attributes

Read

Reads a node

ReadAttributeValue

Parses the attributes value into one or more Text and/or EntityReference node types

ReadXXXX (ReadChar, ReadBoolean, ReadDate, ReadIn32, and so on)

Reads the contents of an element into the specified type including char, double, string, date, and so on

ReadInnerXml

Reads all the content as a string

Skip

Skips the current element

Conclusion

Hope this article would have helped you in understanding Reading XML. See other articles on the website also for further reference.

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.