Recent Posts

Sunday, December 7, 2008

Learn how to read a xml file from vb.Net application with full source code.

This small article will show you how to read the xml file for processing from the vb.net application. This can be very useful for Desktop or Asp.net application to process the xml files and for the beginners who will be willing to work with the web services and xslt.

Check the following steps to find out how to load and read a xml file:

1. Open an application in vb.net. 

2. Place a command button on the form.

3. on the command click write the following code.

 Dim myXMLdoc As New Xml.XmlDataDocument

Dim strFileName As String
            Dim strText As String

'Provide the path of the XML 
            strFileName = "C:test1.xml"
            

            Dim oFile As System.IO.File
            Dim oRead As System.IO.StreamReader

'Read the file from the provided path
            oRead = oFile.OpenText(strFileName)
            strText = oRead.ReadToEnd()

 'Selects the XML Text
        myXMLdoc.LoadXml(strText)
        Dim xnr As Xml.XmlNodeReader
        Dim xn As Xml.XmlNode
        Dim xe As Xml.XmlElement = myXMLdoc.DocumentElement

 'Selects the Single Node
        xn = myXMLdoc.SelectSingleNode("/Data/Name")
        If IsNothing(xn) = False Then
            xnr = New Xml.XmlNodeReader(xn)
            While xnr.Read
                If Not (xnr.Value = "") Then
                    Me.txtLatitude = xnr.Value
                End If
            End While
            xnr.Close()
        End If

Related Posts by Categories




No comments:

Post a Comment