Reputation: 65391
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#
Upvotes: 3
Views: 5371
Reputation: 754478
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
Upvotes: 3