This tutorial will show you how to read and extract data from an XML document with VB.NET using the XmlTextReader class in the System.Xml namespace. To view the article on how to write XML documents, please click here
The XML classes in the System.Xml namespace that can be used to read XML documents are:
- XmlReader
- XmlTextReader
- XmlNodeReader
- There are more readers like XmlDictionaryReader and XmlValidatingReader, but they don’t apply to this tutorial
The code below will provide you with most of the functionality needed to extract data from an XML document. Usually this type of code is used to extract data from an XML RSS feed after which it can be imported to a database. As with writing an XML document (XmlTextWriter and XmlWriter), XmlTextReader and XmlNodeReader are inherited from XmlReader which means XmlReader has a couple of extra features. But for the purpose of this tutorial and in most cases, you’ll only need to use XmlTextReader (or XmlNodeReader).
Code:
<%@ Page Language="VB" %>
<%@ import Namespace="System.Xml" %>
<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
'*** Written by user CWA, CoolWebAwards.com Forums. 18 March 2010
'*** http://forum.coolwebawards.com/
'*** XmlTextReader
Dim XmlReader As New XmlTextReader(server.MapPath("test.xml"))
'*** Uncomment to use XmlReader
'Dim XmlReader As XmlReader = XmlReader.Create(server.MapPath("test.xml"))
'*** Uncomment to use XmlNodeReader
'Dim XMLDocument = New XmlDocument
'XMLDocument.Load(server.MapPath("test.xml"))
'Dim XmlReader = New XmlNodeReader(XMLDocument)
Dim intID, intProductCountTotal As Integer
Dim strPrice, strWeight, strDescription As String
While XmlReader.Read()
'extract the atribute "Id" value when the reader reach the "Product" element.
If XmlReader.Name.ToString() = "Product" Then
If XmlReader.HasAttributes Then
While XmlReader.MoveToNextAttribute()
If XmlReader.Name = "Id" Then
intID = cint(XmlReader.Value)
End If
End While
End If
End If
If XmlReader.Name.ToString() = "price" Then strPrice = trim(XmlReader.ReadString())
If XmlReader.Name.ToString() = "weight" Then strWeight = trim(XmlReader.ReadString())
If XmlReader.Name.ToString() = "description" Then strDescription = trim(XmlReader.ReadString())
'When the end of each <Product> element is reached by the XmlReader, you can extract all the values you need and import them into a database or whatever
'These readers provide forward-only access to XML data, so I choose to extract the data from each of the elements, and then use the values
If XmlReader.Name.ToString() = "Product" AND XmlReader.NodeType.ToString() = "EndElement" Then
response.write("Values at product ID " & cstr(intID) & " is:<br>")
response.write("- price: " & strPrice & "<br>")
response.write("- weight: " & strWeight & "<br>")
response.write("- description: " & strDescription & "<br>")
'Reset the values for next product
intID = 0
strPrice = ""
strWeight = ""
strDescription = ""
intProductCountTotal += 1
End If
End While
XmlReader.Close()
' print the number of <Products> elements counted
response.write("<br>Products in XML doc: " & cstr(intProductCountTotal))
End Sub
</script>
The XML document info used:
Code:
<!--http://forum.coolwebawards.com-->
<Products>
<Product Id="1">
<price>price element text for ID 1</price>
<weight>weight element text for ID 1</weight>
<description>description element text for ID 1</description>
</Product>
<Product Id="2">
<price>price element text for ID 2</price>
<weight>weight element text for ID 2</weight>
<description>description element text for ID 2</description>
</Product>
<Product Id="3">
<price>price element text for ID 3</price>
<weight>weight element text for ID 3</weight>
<description>description element text for ID 3</description>
</Product>
</Products>
The result when reading the document
Code:
Values at product ID 1 is:
- price: price element text for ID 1
- weight: weight element text for ID 1
- description: description element text for ID 1
Values at product ID 2 is:
- price: price element text for ID 2
- weight: weight element text for ID 2
- description: description element text for ID 2
Values at product ID 3 is:
- price: price element text for ID 3
- weight: weight element text for ID 3
- description: description element text for ID 3
Products in XML doc: 3
Click here for more information on how to write an XML document using vb.net