xml file parsing in vb
TRANSCRIPT
-
7/24/2019 XML File Parsing in VB
1/4
9/21/2015 XML File Parsing in VB.NET - CodeProject
data:text/htmlcharset=utf-8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%2
Rate:Pratik Desai, 21 Aug 2003
XMLFile Parsing in VB.NET
Exploring various methods to parse an XMLfile in a .NET environment
Is your email address OK?You are signed up for our newsletters but your email address is
either unconfirmed, or has not been reconfirmed in a long time. Please click hereto have a
confirmation email sent so we can confirm your email address and start sending you
newsletters again. Alternatively, you can update your subscriptions.
IntroductionParsing XMLfiles has always been time consuming and sometimes tricky. .NET framework provides powerful
new ways of parsing XML. The various techniques know to parse xmlfiles with .NET framework are
using XmlTextReader,XmlDocument, XmlSerializer, DataSet and XpathDocument. I will explore
theXmlTextReader and XmlDocument approach here.
The XmlFile
Figure 1 outlines the xmlfile that will be parsed.
Hide Copy Code
Tom Smith Dale Smith
Figure1: Xmlfile
Parsing XMLwith XMLTextReader
Using XmlTextReader is appropriate when the structure of the XMLfile is relatively simple. Parsing
with XmlTextReader gives you a pre .net feel as you sequentially walk through the file using Read() and
4.62 72 votes
http://www.codeproject.com/script/Membership/Subscribe.aspx?rp=%2fArticles%2f4826%2fXML-File-Parsing-in-VB-NEThttp://www.codeproject.com/script/Membership/View.aspx?mid=543908http://www.codeproject.com/script/Membership/View.aspx?mid=543908http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET#http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET#http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET#http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET#http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET#http://www.codeproject.com/script/Membership/Subscribe.aspx?rp=%2fArticles%2f4826%2fXML-File-Parsing-in-VB-NEThttp://www.codeproject.com/script/Membership/SendConfirmRequest.aspx?rp=%2fArticles%2f4826%2fXML-File-Parsing-in-VB-NEThttp://www.codeproject.com/script/Membership/View.aspx?mid=543908 -
7/24/2019 XML File Parsing in VB
2/4
9/21/2015 XML File Parsing in VB.NET - CodeProject
data:text/htmlcharset=utf-8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%2
get data usingGetAttribute() andReadElementString() methods. Thus while
usingXmlTextReader it is up to the developer to keep track where he is in the Xmlfile
and Read() correctly. Figure 2 below outlines parsing of xmlfile withXmlTextReader
Hide Shrink Copy Code
Imports System.IOImports System.XmlModule ParsingUsingXmlTextReaderSub Main()
Dim m_xmlr AsXmlTextReader 'Create the XMLReader m_xmlr = NewXmlTextReader("C:\Personal\family.xml") 'Disable whitespace so that you don't have to read over whitespaces
m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE 'read thexml declaration and advance to family tag m_xmlr.Read() 'read the family tag m_xmlr.Read() 'Load the Loop WhileNot m_xmlr.EOF 'Go to the name tag
m_xmlr.Read()
'if not start element exit while loop IfNot m_xmlr.IsStartElement() Then ExitWhile EndIf 'Get the Gender Attribute Value Dim genderAttribute = m_xmlr.GetAttribute("gender") 'Read elements firstname and lastname
m_xmlr.Read() 'Get the firstName Element Value Dim firstNameValue = m_xmlr.ReadElementString("firstname") 'Get the lastName Element Value Dim lastNameValue = m_xmlr.ReadElementString("lastname") 'Write Result to the Console
Console.WriteLine("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _ & lastNameValue) Console.Write(vbCrLf) EndWhile 'close the reader m_xmlr.Close()EndSubEndModule
Figure 2: XmlParsing with XmlTextReader
Parsing XMLwith XmlDocumentThe XmlDocument class is modeled based on Document Object Model.XmlDocument class is appropriate if
you need to extract data in a nonsequential manner. Figure 3 below outlines parsing of xmlfile
with XmlDocument
Hide Shrink Copy Code
Imports System.IOImports System.XmlModule ParsingUsingXmlDocumentSub Main()
-
7/24/2019 XML File Parsing in VB
3/4
9/21/2015 XML File Parsing in VB.NET - CodeProject
data:text/htmlcharset=utf-8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%2
Try Dim m_xmld AsXmlDocument Dim m_nodelist AsXmlNodeList Dim m_node AsXmlNode 'Create the XML Document m_xmld = NewXmlDocument() 'Load the Xml file m_xmld.Load("C:\CMS\Personal\family.xml")
'Get the list of name nodesm_nodelist = m_xmld.SelectNodes("/family/name")
'Loop through the nodes ForEach m_node In m_nodelist 'Get the Gender Attribute Value Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value 'Get the firstName Element Value
Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText 'Get the lastName Element Value
Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText 'Write Result to the Console
Console.Write("Gender: " & genderAttribute _
& " FirstName: " & firstNameValue & " LastName: " _ & lastNameValue) Console.Write(vbCrLf)
Next
Catch errorVariable As Exception 'Error trapping Console.Write(errorVariable.ToString())
EndTryEndSubEndModule
Figure 3: XmlParsing with XmlDocument
Compilation and Result
Make sure you have vbc.exein your path. From the command prompt go
toC:\Personal>. Compile ParsingUsingXmlTextReader.vbandParsingUsingXmlDocument.vb.
Hide Copy Code
C:\Personal>vbc /out:ParsingUsingXmlTextReadervb.exe ParsingUsingXmlTextReader.vbC:\Personal>vbc /out:ParsingUsingXmlDocumentvb.exe ParsingUsingXmlDocument.vb
When you run the individual program
Hide Copy Code
C:\Personal>ParsingUsingXmlTextReadervb.exe
OR
Hide Copy Code
C:\Personal>ParsingUsingXmlDocumentvb.exe
You will see the following result for both
Hide Copy Code
Gender: Male FirstName: Tom LastName: Smith
-
7/24/2019 XML File Parsing in VB
4/4
9/21/2015 XML File Parsing in VB.NET - CodeProject
data:text/htmlcharset=utf-8,%3Cdiv%20class%3D%22header%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%3B%20border%3A%2
Gender: Female FirstName: Dale LastName: Smith
Conclusion
There are different ways to parse XMLfiles and the best method depends on your situation and the
programming style preferred.
License
This article has no explicit license attached to it but may contain usage terms in the article text or the
download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found he
http://www.codeproject.com/info/Licenses.aspx