Parsing XML from a XmlDocument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    Parsing XML from a XmlDocument

    Hi,

    I've created an HTTP connection with a server. I send a WebRequest in XML to the server (see strReq in code) and it returns me a WebResponse. This response is inserted in a string, which is then loaded into a XmlDocument. How can I parse this XmlDocument? I tried a few things, but I didn't get it to work. The output which is in de XmlDocument is like this:

    <?xml version="1.0" encoding="iso-8859-1" ?>
    <antwoord type="ack">T08-13995</antwoord>

    I need the piece of code that is on the second line between <antwoord> and </antwoord>. Does anyone have an idea on how to go about that?

    Below is the code I use for the WebRequest and the WebResponse:

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim sp_response As XmlDocument = New XmlDocument()
            Dim strReq, strRsp, url As String
            Dim httpReq As HttpWebRequest
            Dim httpRsp As HttpWebResponse
            Dim streamReq, streamRsp As Stream
            Dim sw As StreamWriter
            Dim sr As StreamReader
            url = "http://..."
            Try
                httpReq = WebRequest.Create(url)
                httpReq.Method = "POST"
                httpReq.KeepAlive = False
                httpReq.UserAgent = Nothing
                httpReq.ContentType = "text/xml"
    
                streamReq = httpReq.GetRequestStream
                sw = New StreamWriter(streamReq)
                strReq = "<?xml version='1.0' encoding='UTF-8'?><lmsvraag nummer='464312'/>"
                sw.Write(strReq)
                sw.Close()
                httpRsp = httpReq.GetResponse
                streamRsp = httpRsp.GetResponseStream
                sr = New StreamReader(streamRsp)
                strRsp = sr.ReadToEnd
                httpRsp.Close()
                sp_response.LoadXml(strRsp)
                Debug.WriteLine(strRsp)
            Catch ex As Exception
                Debug.WriteLine("EXCEPTION")
                Debug.WriteLine("Error #" + ex.Message)
                Debug.WriteLine("Error reported by " + ex.Source)
            End Try
        End Sub
    Thnx for your help in advance!

    Cheers,

    Steven
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi ,

    You can try the following code in the code that you have written:
    e.g.

    Code:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim sp_response As XmlDocument = New XmlDocument()
            Dim strReq, strRsp, url As String
            Dim httpReq As HttpWebRequest
            Dim httpRsp As HttpWebResponse
            Dim streamReq, streamRsp As Stream
            Dim sw As StreamWriter
            Dim sr As StreamReader
            url = "http://..."
            Try
                httpReq = WebRequest.Create(url)
                httpReq.Method = "POST"
                httpReq.KeepAlive = False
                httpReq.UserAgent = Nothing
                httpReq.ContentType = "text/xml"
    
                streamReq = httpReq.GetRequestStream
                sw = New StreamWriter(streamReq)
                strReq = "<?xml version='1.0' encoding='UTF-8'?><lmsvraag nummer='464312'/>"
                sw.Write(strReq)
                sw.Close()
                httpRsp =      httpReq.GetResponse
                streamRsp =  httpRsp.GetResponseStream
                sr =  New StreamReader(streamRsp)
                strRsp = sr.ReadToEnd
                httpRsp.Close()
                sp_response.LoadXml(strRsp)
    
    
                [B]'' Process xml from xmldocument '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
                
                Dim xmlNodeList As XmlNodeList
              
                  ''Get the xmlNodeList with the name as "antwoord"
               
                  xmlNodeList = _doc.GetElementsByTagName("antwoord")
    
    
                 For index As Integer = 0 To xmlNodeList.Count - 1
                    ''Get the value of each selected node
                   MsgBox(xmlNodeList.Item(index).InnerText)
                Next[/B] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''           
      
                Debug.WriteLine(strRsp)
            Catch ex As Exception
                Debug.WriteLine("EXCEPTION")
                Debug.WriteLine("Error #" + ex.Message)
                Debug.WriteLine("Error reported by " + ex.Source)
            End Try
        End Sub

    Comment

    • MrMancunian
      Recognized Expert Contributor
      • Jul 2008
      • 569

      #3
      Hi, thanks a lot, works like charm! Now something else:

      The response by the server is, like I said, as follows:

      <?xml version="1.0" encoding="iso-8859-1" ?>
      <antwoord type="ack">T08-13995</antwoord>

      The type in the Antwoord-node can be either "ack", as shown above, or it can be "error". Is there any way to check what the value of the type is?

      Thnx,

      Steven

      Comment

      • MrMancunian
        Recognized Expert Contributor
        • Jul 2008
        • 569

        #4
        Has anyone got an idea on my last question?

        Thnx in advance,

        Steven

        Comment

        Working...