473,407 Members | 2,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Parsing XML from a XmlDocument

MrMancunian
569 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         Dim sp_response As XmlDocument = New XmlDocument()
  3.         Dim strReq, strRsp, url As String
  4.         Dim httpReq As HttpWebRequest
  5.         Dim httpRsp As HttpWebResponse
  6.         Dim streamReq, streamRsp As Stream
  7.         Dim sw As StreamWriter
  8.         Dim sr As StreamReader
  9.         url = "http://..."
  10.         Try
  11.             httpReq = WebRequest.Create(url)
  12.             httpReq.Method = "POST"
  13.             httpReq.KeepAlive = False
  14.             httpReq.UserAgent = Nothing
  15.             httpReq.ContentType = "text/xml"
  16.  
  17.             streamReq = httpReq.GetRequestStream
  18.             sw = New StreamWriter(streamReq)
  19.             strReq = "<?xml version='1.0' encoding='UTF-8'?><lmsvraag nummer='464312'/>"
  20.             sw.Write(strReq)
  21.             sw.Close()
  22.             httpRsp = httpReq.GetResponse
  23.             streamRsp = httpRsp.GetResponseStream
  24.             sr = New StreamReader(streamRsp)
  25.             strRsp = sr.ReadToEnd
  26.             httpRsp.Close()
  27.             sp_response.LoadXml(strRsp)
  28.             Debug.WriteLine(strRsp)
  29.         Catch ex As Exception
  30.             Debug.WriteLine("EXCEPTION")
  31.             Debug.WriteLine("Error #" + ex.Message)
  32.             Debug.WriteLine("Error reported by " + ex.Source)
  33.         End Try
  34.     End Sub
  35.  
Thnx for your help in advance!

Cheers,

Steven
Oct 17 '08 #1
3 1507
shweta123
692 Expert 512MB
Hi ,

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

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         Dim sp_response As XmlDocument = New XmlDocument()
  3.         Dim strReq, strRsp, url As String
  4.         Dim httpReq As HttpWebRequest
  5.         Dim httpRsp As HttpWebResponse
  6.         Dim streamReq, streamRsp As Stream
  7.         Dim sw As StreamWriter
  8.         Dim sr As StreamReader
  9.         url = "http://..."
  10.         Try
  11.             httpReq = WebRequest.Create(url)
  12.             httpReq.Method = "POST"
  13.             httpReq.KeepAlive = False
  14.             httpReq.UserAgent = Nothing
  15.             httpReq.ContentType = "text/xml"
  16.  
  17.             streamReq = httpReq.GetRequestStream
  18.             sw = New StreamWriter(streamReq)
  19.             strReq = "<?xml version='1.0' encoding='UTF-8'?><lmsvraag nummer='464312'/>"
  20.             sw.Write(strReq)
  21.             sw.Close()
  22.             httpRsp =      httpReq.GetResponse
  23.             streamRsp =  httpRsp.GetResponseStream
  24.             sr =  New StreamReader(streamRsp)
  25.             strRsp = sr.ReadToEnd
  26.             httpRsp.Close()
  27.             sp_response.LoadXml(strRsp)
  28.  
  29.  
  30.             '' Process xml from xmldocument '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
  31.  
  32.             Dim xmlNodeList As XmlNodeList
  33.  
  34.               ''Get the xmlNodeList with the name as "antwoord"
  35.  
  36.               xmlNodeList = _doc.GetElementsByTagName("antwoord")
  37.  
  38.  
  39.              For index As Integer = 0 To xmlNodeList.Count - 1
  40.                 ''Get the value of each selected node
  41.                MsgBox(xmlNodeList.Item(index).InnerText)
  42.             Next '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''           
  43.  
  44.             Debug.WriteLine(strRsp)
  45.         Catch ex As Exception
  46.             Debug.WriteLine("EXCEPTION")
  47.             Debug.WriteLine("Error #" + ex.Message)
  48.             Debug.WriteLine("Error reported by " + ex.Source)
  49.         End Try
  50.     End Sub
  51.  
Oct 17 '08 #2
MrMancunian
569 Expert 512MB
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
Oct 17 '08 #3
MrMancunian
569 Expert 512MB
Has anyone got an idea on my last question?

Thnx in advance,

Steven
Oct 20 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert Mark Bram | last post by:
Hi all! I want to use asp to do text find and replace with XHTML elements.. for example, let's say I have the following XHTML: <note> <from id="123">Jani</from> <to>Tove</to>...
6
by: Thomas Polan | last post by:
Sorry if this has been posted before... I am receiving XML messages over a TCP client. Messages vary in size and sometimes can arrive in groups. Thus, I am not guaranteed to receive a full...
4
by: sunil | last post by:
I am creating a XML document which opens fine in IE. Implies MSXML thinks it is a well formed document. But when I try to load this document in VB.net using the following code Dim doc As New...
4
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the...
2
by: Anthony Boudouvas | last post by:
Hi to all, i have a very simple XML file that i present to a user and i will allow him/her to manualy edit it and send it back to a listening server. What is the best -simple- way to parse it...
8
by: Dinçer | last post by:
I need to parse an XML file and print the contents of it on an aspx page. My XML file structure is like this: <QueryResult> <messageNo>1</messageNo> <messageText /> <HERE>...
2
by: Mark | last post by:
Hi... We've been doing some basic performance testing comparing asp, asp.net, mono, and php. One of the basic tests is on simply parsing an xml document and streaming the result back to the...
3
by: george | last post by:
Hi, I am parsing a XML string (see code below) by loading into XMLDocument and selecting certain node (Result) and loop through it. Instead of using item(), is there a way to get the element...
1
by: danep | last post by:
Hi, I'm fairly new to AJAX, but I've been able to retrieve HTML and plain-text documents without any trouble. However, I haven't figured out how to retrieve it in XML format. Basically, here's...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.