Introduction
Parsing XML files has always been time consuming and sometimes tricky. .NET framework provides powerful new ways of parsing XML. The various techniques know to parse xml files with .NET framework are using XmlTextReader
, XmlDocument
, XmlSerializer
, DataSet
and XpathDocument
. I will explore the XmlTextReader
and XmlDocument
approach here.
The Xml File
Figure 1 outlines the xml file that will be parsed.
="1.0"="UTF-8"
<family>
<name gender="Male">
<firstname>Tom</firstname>
<lastname>Smith</lastname>
</name>
<name gender="Female">
<firstname>Dale</firstname>
<lastname>Smith</lastname>
</name>
</family>
Figure1: Xml file
Parsing XML with XMLTextReader
Using XmlTextReader
is appropriate when the structure of the XML file is relatively simple. Parsing with XmlTextReader
gives you a pre .net feel as you sequentially walk through the file using Read()
and get data using GetAttribute()
andReadElementString()
methods. Thus while using XmlTextReader
it is up to the developer to keep track where he is in the Xml file and Read()
correctly. Figure 2 below outlines parsing of xml file with XmlTextReader
Imports System.IO
Imports System.Xml
Module ParsingUsingXmlTextReader
Sub Main()
Dim m_xmlr As XmlTextReader
m_xmlr = New XmlTextReader("C:\Personal\family.xml")
m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
m_xmlr.Read()
m_xmlr.Read()
While Not m_xmlr.EOF
m_xmlr.Read()
If Not m_xmlr.IsStartElement() Then
Exit While
End If
Dim genderAttribute = m_xmlr.GetAttribute("gender")
m_xmlr.Read()
Dim firstNameValue = m_xmlr.ReadElementString("firstname")
Dim lastNameValue = m_xmlr.ReadElementString("lastname")
Console.WriteLine("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
End While
m_xmlr.Close()
End Sub
End Module
Figure 2: Xml Parsing with XmlTextReader
Parsing XML with XmlDocument
The XmlDocument
class is modeled based on Document Object Model. XmlDocument
class is appropriate if you need to extract data in a non-sequential manner. Figure 3 below outlines parsing of xml file with XmlDocument
Imports System.IO
Imports System.Xml
Module ParsingUsingXmlDocument
Sub Main()
Try
Dim m_xmld As XmlDocument
Dim m_nodelist As XmlNodeList
Dim m_node As XmlNode
m_xmld = New XmlDocument()
m_xmld.Load("C:\CMS\Personal\family.xml")
m_nodelist = m_xmld.SelectNodes("/family/name")
For Each m_node In m_nodelist
Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _
& lastNameValue)
Console.Write(vbCrLf)
Next
Catch errorVariable As Exception
Console.Write(errorVariable.ToString())
End Try
End Sub
End Module
Figure 3: Xml Parsing with XmlDocument
Compilation and Result
Make sure you have vbc.exe in your path. From the command prompt go to C:\Personal>. Compile ParsingUsingXmlTextReader.vb and ParsingUsingXmlDocument.vb.
C:\Personal>vbc /out:ParsingUsingXmlTextReadervb.exe ParsingUsingXmlTextReader.vb
C:\Personal>vbc /out:ParsingUsingXmlDocumentvb.exe ParsingUsingXmlDocument.vb
When you run the individual program
C:\Personal>ParsingUsingXmlTextReadervb.exe
OR
C:\Personal>ParsingUsingXmlDocumentvb.exe
You will see the following result for both
Gender: Male FirstName: Tom LastName: Smith
Gender: Female FirstName: Dale LastName: Smith
Conclusion
There are different ways to parse XML files and the best method depends on your situation and the programming style preferred.
Architects softwares with emerging technologies that will save time and increase productivity.