[Vb.net] XML File Parsing in VB. NET, vb. netparsing

Source: Internet
Author: User
Tags xml reader

[Vb.net] XML File Parsing in VB. NET, vb. netparsing
Introduction

Parsing XML files has always been time consuming and sometimes tricky.. NET framework provides powerful new ways of parsing XML. The varous techniques know to parse xml files with. NET framework are usingXmlTextReader,XmlDocument,XmlSerializer,DataSetAndXpathDocument. I will need eXmlTextReaderAndXmlDocumentApproach here.

The Xml File

Figure 1 outlines the xml file that will be parsed.

<?xml version="1.0" encoding="UTF-8"?>
<family>
  <name gender="Male">
    <firstname>Tom</firstname>
    <lastname>Smith</lastname>
  </name>
  <name gender="Female">
    <firstname>Dale</firstname>
    <lastname>Smith</lastname>
  </name>
</family>
Parsing XML with XMLTextReader

UsingXmlTextReaderIs appropriate when the structure of the XML file is relatively simple. ParsingXmlTextReaderGives you a pre. net feel as you sequentially walk through the file usingRead()And get data usingGetAttribute()AndReadElementString()Methods. Thus while usingXmlTextReaderIt is up to the developer to keep track where he is in the Xml file andRead()Correctly. Figure 2 below outlines parsing of xml fileXmlTextReader

 1 Imports System.IO
 2 Imports System.Xml
 3 Module ParsingUsingXmlTextReader
 4 Sub Main()
 5   Dim m_xmlr As XmlTextReader
 6   'Create the XML Reader
 7   m_xmlr = New XmlTextReader("C:\Personal\family.xml")
 8   'Disable whitespace so that you don't have to read over whitespaces
 9   m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
10   'read the xml declaration and advance to family tag
11   m_xmlr.Read()
12   'read the family tag
13   m_xmlr.Read()
14   'Load the Loop
15 While Not m_xmlr.EOF
16     'Go to the name tag
17     m_xmlr.Read()
18     'if not start element exit while loop
19     If Not m_xmlr.IsStartElement() Then
20       Exit While
21     End If
22     'Get the Gender Attribute Value
23     Dim genderAttribute = m_xmlr.GetAttribute("gender")
24     'Read elements firstname and lastname
25     m_xmlr.Read()
26     'Get the firstName Element Value
27     Dim firstNameValue = m_xmlr.ReadElementString("firstname")
28     'Get the lastName Element Value
29     Dim lastNameValue = m_xmlr.ReadElementString("lastname")
30     'Write Result to the Console
31     Console.WriteLine("Gender: " & genderAttribute _
32       & " FirstName: " & firstNameValue & " LastName: " _
33       & lastNameValue)
34     Console.Write(vbCrLf)
35   End While
36   'close the reader
37   m_xmlr.Close()
38 End Sub
39 End Module
Parsing XML with XmlDocument

TheXmlDocumentClass is modeled based on Document Object Model.XmlDocumentClass is appropriate if you need to extract data in a non-sequential manner. Figure 3 below outlines parsing of xml fileXmlDocument

 1 Imports System.IO  2 Imports System.Xml
 3 Module ParsingUsingXmlDocument
 4 Sub Main()
 5   Try
 6     Dim m_xmld As XmlDocument
 7     Dim m_nodelist As XmlNodeList
 8     Dim m_node As XmlNode
 9     'Create the XML Document
10     m_xmld = New XmlDocument()
11     'Load the Xml file
12     m_xmld.Load("C:\CMS\Personal\family.xml")
13     'Get the list of name nodes  14     m_nodelist = m_xmld.SelectNodes("/family/name")
15     'Loop through the nodes
16     For Each m_node In m_nodelist
17       'Get the Gender Attribute Value
18       Dim genderAttribute = m_node.Attributes.GetNamedItem("gender").Value
19       'Get the firstName Element Value
20       Dim firstNameValue = m_node.ChildNodes.Item(0).InnerText
21       'Get the lastName Element Value
22       Dim lastNameValue = m_node.ChildNodes.Item(1).InnerText
23       'Write Result to the Console
24       Console.Write("Gender: " & genderAttribute _
25         & " FirstName: " & firstNameValue & " LastName: " _
26         & lastNameValue)
27       Console.Write(vbCrLf)
28     Next
29   Catch errorVariable As Exception
30     'Error trapping
31     Console.Write(errorVariable.ToString())
32   End Try
33 End Sub
34 End Module

You will see the following result for both

Gender: Male FirstName: Tom LastName: Smith

Gender: Female FirstName: Dale LastName: Smith

 

Http://www.codeproject.com/Articles/4826/XML-File-Parsing-in-VB-NET


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.