ARTICLE

ADO.NET XML Parser in VB.NET

Posted by Munesh Sharma Articles | ADO.NET in VB.NET April 06, 2011
In this articale we will discuss about the xml parser in VB.NET
Reader Level:


XML in ADO.NET:
XML has become one of the most well known and important technologies for representing data. Regarded by many as the key to interoperability and flexibility across applications and  platforms,  XML support is embedded deeply into the Microsoft. NET Framework, and especially in to ADO.NET.
XML Parser:
An XML Parser is a program that read an XML document and extracts the data and data descriptions from the XML parser enables you to programmatically work with an xml document having to manually parse the file.

  •  ParsingUsingXmlTextReader
  •  ParsingUsingXmlTextWriter
  •  ParsingUsingXmlDocument

Steps to Crate ParsingUsingXmlTextReader in VB.NET:
  • Microsoft visual studio>New project>console Application.
  • Create Xml File:

      <?xml version="1.0" encoding="UTF-8" ?>
     
    <family>
     <name gender="Male">
     
    <firstname>Ram</firstname>
     
    <lastname>Singh</lastname>
     
    </name>
     <name gender="Female">
     
    <firstname>Dena</firstname>
     
    <lastname>Singh</lastname>
     
    </name>
     
    </family>

  • To Save file family.xml.  

  • Write these code

    Imports System.IO
    Imports
    System.Xml
    Module
    ParsingUsingXmlTextReader
        Sub Main()
            Dim m_xmlr As XmlTextReader
            'Create the XML Reader
            m_xmlr = New XmlTextReader("D:\family.xml")
            'Disable whitespace so that you don't have to read over whitespaces
            m_xmlr.WhiteSpaceHandling = WhiteSpaceHandling.NONE
            'read the xml declaration and advance to family tag
            m_xmlr.Read()
            'read the family tag
            m_xmlr.Read()
            'Load the Loop
            While Not m_xmlr.EOF
                'Go to the name tag
                m_xmlr.Read()
                'if not start element exit while loop
                If Not m_xmlr.IsStartElement() Then
                    Exit While
                End If
                '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)
            End While
            'close the reader
            m_xmlr.Close()
        End Sub

    End
    Module

    Coding for ParsingUsingXmlDocument:

    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
                'Create the XML Document
                m_xmld = New XmlDocument()
                'Load the Xml file
                m_xmld.Load("D:\family.xml")
                'Get the list of name nodes
                m_nodelist = m_xmld.SelectNodes("/family/name")
                'Loop through the nodes
                For Each 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())
            End Try
        End Sub

    End
    Module
     

    Output:

             Output-of-reader.gif

     


     

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor