Reading xml from aspx web page

前端 未结 1 1926
轮回少年
轮回少年 2021-01-06 18:48

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

相关标签:
1条回答
  • 2021-01-06 19:13

    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

    0 讨论(0)
提交回复
热议问题