Read XML file from file server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sindhuvenkat
    New Member
    • Sep 2009
    • 7

    Read XML file from file server

    Hi,

    I need to read a xml file present in a file server/web server from stand alone application. Is it possible? If so, please give me an example or the method to read.

    Thanks in advance
    Sindhu
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    Could you please ellaborate more on this ?

    you want to read xml as it is or into some other format ?

    Thanks!

    Comment

    • sashi
      Recognized Expert Top Contributor
      • Jun 2006
      • 1749

      #3
      Hi Sindhu,

      Yes, it is possible. There are 3 methods known to me, the list goes on as per below;
      1. XmlTextReader
      2. XmlDocument
      3. XPath


      Assuming this is the format of your xml file
      Code:
      <?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>
      Code:
      Imports System.IO
      Imports System.Xml
      
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      
        Dim m_xmlr As XmlTextReader
        'Create the XML Reader
      
        m_xmlr = New XmlTextReader("name_of_the_xml_file_to_read.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

      Comment

      Working...