Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading xml from aspx web page

We have to read data from a aspx page. When we call the page with a query string, it returns an xml document with data that matches the query string.

We have an XSD that matches the xml that we get back.

I am thinking that we can read the xml document out of the http response. Will this work?

How can we bind the XML with the XSD, such that we can treat the XML document as if it were strongly typed?

Thanks,

Shiraz

Update:

Found this link on how to deserialize

Deserializing XML to Objects in C#

like image 953
Shiraz Bhaiji Avatar asked Aug 06 '26 14:08

Shiraz Bhaiji


1 Answers

Well, basically, you can request an XML document something like this (no try/catch here - but you should definitely add that!):

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";  // or GET - depends 

myRequest.ContentType = "text/xml; encoding=utf-8";
myRequest.ContentLength = data.Length;

using (Stream reqStream = myRequest.GetRequestStream())
{
  // Send the data.
  reqStream.Write(data, 0, data.Length);
  reqStream.Close();
}

// Get Response
WebResponse myResponse;

myResponse = myRequest.GetResponse();
XmlDocument _xmlDoc = new XmlDocument();

using (Stream responseStream = myResponse.GetResponseStream())
{
   _xmlDoc.Load(responseStream);
}   

Whether you have a GET or POST depends on your scenario - in a GET, you won't have request data going out.

Once you have your XML back as a XmlDocument, you can either validate that against the XML schema, or just try to deserialize it into the type that is represented by the XSD schema you have.

If that works --> the XML you got is valid and OK. If not, you'll get an exception on deserialization.

Marc

like image 99
marc_s Avatar answered Aug 09 '26 03:08

marc_s



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!