473,811 Members | 2,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Parsing XML from a XmlDocument

MrMancunian
569 Recognized Expert Contributor
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 1539
shweta123
692 Recognized Expert Contributor
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 Recognized Expert Contributor
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 Recognized Expert Contributor
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
1492
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> <message>Norwegian: æøå. French: êèé</message> </note>
6
13513
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 message in a single read from my socket. I am loading each message into an XmlDocument after it arrives. I need a way to make sure I have a complete XML message before I load it into an XmlDocument or it will throw an exception. I am currently...
4
12003
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 XmlDocument doc.Load("C:\Projects\SQLXML\corc.xml") I get the following error: "System.Xml.XmlException: An unexpected end of file parsing CDATA has
4
2465
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 nodes in the instance unqualified. An example of the document is below: <?xml version="1.0" encoding="utf-8" ?> <filingreceipt xmlns="http://www.disclosureusa.org/filingreceipt.xsd"> <cpofilingnumber>TX2004043037501</cpofilingnumber>...
2
5043
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 so it does not contain anything invalid after edit ?? I just need a simple data validation, no xml-schemas etc...
8
1925
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> <ExternalIntegrator><Entities> <name>John</name> <surname>Carpenter</surname>
2
1415
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 client. I never would have thought of it, but someone in our group had heard that using XmlDocument.Load (Server.MapPath (file)); was slower than actually creating the stream, the XmlTextReader, and passing the reader to the XmlDocument.Load();.
3
2949
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 value by name? Also, is there a way to using the XmlTextReader to move to Search/Result and then loop though each node?
1
2080
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 the script that's supposed to retrieve and parse the data when you pass it the url of the page generating the XML data: <script type="text/javascript"> var xmlHttp function getresponse(url)
0
9728
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10648
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9205
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7670
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3867
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3018
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.