Groups > Asp .Net > HttpHandlers and HttpModules > Re: Using http handler to return an xml document server side




Using http handler to return an xml document server side

Using http handler to return an xml document server side
Wed, 20 Feb 2008 16:49:45 +000
Hi I using .net 2 I am trying to to send a request to another domain to recieve
an xml document. When I recieve this xml document i will use it to populate
controls on the page before i send the page to the client. My question is what
is the best way to do this.

I do not want to use the client for this as the are all sorts of incompatbility
issues depending on browser. Instead I would like to do this at the server and
then post back the results to the client.

i.e. If i type a url into the browser window directly i get returned back to me
an xml document. for example (PLEASE DO NOT CLICK THIS LINK AS IT DOES NOT WORK
IT IS AN EXAMPLE ONLY)
http://xxx.co.uk/somefolder/somepage?id=MG02

This returns an xml doc like so and displays it in the browser<?xml
version="1.0" encoding="UTF-8"
?>-<DATASET>-<DVLA>-<VEHICLE> <VRM>XXXX</VRM>
<VIN>XXXYYYXXX</VIN> <MAKE_CODE>MH</MAKE_CODE>
<MODEL_CODE>400</MODEL_CODE> <CC>1995</CC>
<COLOUR>J</COLOUR>
<LAST_COLOUR_DATE>01-JAN-0001</LAST_COLOUR_DATE>
<PREV_COLOURS>0</PREV_COLOURS>   </VEHICLE>-<MAKE>
<CODE>MH1</CODE> <DESC>BMW</DESC>
</MAKE>-<MODEL> <CODE>MH2400</CODE> <DESC>320I M
SPORT</DESC> </MODEL>-<BODY> <CODE>05</CODE>
<DESC>COUPE</DESC> </BODY>-<COLOUR>
<CODE>J</CODE> <DESC>BLUE</DESC> </COLOUR>
</DVLA> <PITO/> </DATASET>  I would like to send an http
request from the server in the aspx page that loads this xml feed into an xml
object so I can interrogate the returned fields and populate edit box controls
on the form before I post the form back to the client.Then when the client loads
the page the fields will then automatically be populated for him/her. Any Help
in the matter will be greatly appreciated. Mick!Forever onwards is the call!
Post Reply
Re: Using http handler to return an xml document server side
Fri, 22 Feb 2008 05:57:32 +000
Hi,

From your description, you just want to make a request to your file and retrieve
the xml return, right?

If so, you can use HttpWebRequest class to make a request to your target file,
and retrieve your XML return. See the following sample:

byte[] buf = new byte[38192];
        HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(“TargetUrl”);
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream resStream = response.GetResponseStream();
        int count = resStream.Read(buf, 0, buf.Length);
        string intervalue = Encoding.Default.GetString(buf, 0, count);
        resStream.Close();

After that you can use XMLDocument object to handle with your XML return. 

Read the attribute in a specific node.
   XmlDocument doc=new XmlDocument();
   doc.LoadXML(TheXMLStringReturnedFromWebRequest);
   XmlNode xnuser=doc.SelectSingleNode("userdata");
   string flag=xnuser.Attributes["createuser"].InnerText;

Read value in a specific node.

   XmlDocument doc=new XmlDocument();
   doc.LoadXML(TheXMLStringReturnedFromWebRequest);
   XmlNode xnserver =
doc.SelectSingleNode("userdata/dataconnection/server");

Hope that helps. Thanks.
Post Reply
about | contact