Need to retrieve nodes from xml page.

N

Niclas Colleen

Hi all,

I'm having this problem, the following code is used to request a page
where I want to be able to read the response, which is an xml page:


string sUrl = "http://www.thirdpartysendingsms.com";
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(sUrl);
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream());
string sResponse = sr.ReadToEnd();


sResponse contains: bostaddirekt97579

While the whole page I've requested looks like this:


<?xml version="1.0" encoding="UTF-8" ?>
- <response code="0" description="Message processed">
<error code="3" description="Could not find any matching prefix for
+99999">bostaddirekt97579</error>
</response>


Now what I want is to be able to extract the error code, in Java I see
them using something like:


URL myUrl = new URL(sUrl);
HttpURLConnection myConn = (HttpURLConnection) myUrl.openConnection();
InputStream in = myConn.getInputStream();
Document myResponse = documentBuilder.parse(in);
Element root = myResponse.getDocumentElement();


And then from there one works with the root to find a specific node, is

there anything equivalent in c#?


Thanks for any pointers!
 
J

Jianwei Sun

Niclas said:
Hi all,

I'm having this problem, the following code is used to request a page
where I want to be able to read the response, which is an xml page:


string sUrl = "http://www.thirdpartysendingsms.com";
HttpWebRequest myRequest =(HttpWebRequest)WebRequest.Create(sUrl);
WebResponse myResponse = myRequest.GetResponse();
StreamReader sr = new StreamReader(myResponse.GetResponseStream());
string sResponse = sr.ReadToEnd();


sResponse contains: bostaddirekt97579

While the whole page I've requested looks like this:


<?xml version="1.0" encoding="UTF-8" ?>
- <response code="0" description="Message processed">
<error code="3" description="Could not find any matching prefix for
+99999">bostaddirekt97579</error>
</response>


Now what I want is to be able to extract the error code, in Java I see
them using something like:


URL myUrl = new URL(sUrl);
HttpURLConnection myConn = (HttpURLConnection) myUrl.openConnection();
InputStream in = myConn.getInputStream();
Document myResponse = documentBuilder.parse(in);
Element root = myResponse.getDocumentElement();


And then from there one works with the root to find a specific node, is

there anything equivalent in c#?


Thanks for any pointers!

Check out system.xml. There are a whole set of classes to read/write xml
document. You probably want to look for something like XmlReader..
 
N

Niclas Colleen

Thanks Jianwei, This was exactly what I was looking for, I'll post my
code below:

//Request page
HttpWebRequest Req =(HttpWebRequest)WebRequest.Create(sURL);
WebResponse Res = Req.GetResponse();
XmlTextReader xReader = new XmlTextReader(Res.GetResponseStream());

//Examine return code
while(xReader.Read())
{
if(xReader.Name == "error")
{
//Error!!!
}
}


Jianwei Sun skrev:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top