4

If someone posts XML from an application to my ASP.NET page, how can I parse it and give the response back in XML format?

Sample client code posting XML to my URL:

WebRequest req = null;
WebResponse rsp = null;
string uri = "https://beta.abc.company.com/mypage.aspx";
req = WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "text/xml";
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.WriteLine(txtXML.Text.ToString());
writer.Close();
rsp = req.GetResponse();

How could I parse XML from mypage.aspx and give the response as XML?

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Mou
  • 15,673
  • 43
  • 156
  • 275

1 Answers1

5

You could read the XML from the request stream. So inside your mypage.aspx:

protected void Page_Load(object sender, EventAgrs e)
{
    using (var reader = new StreamReader(Request.InputStream))
    {
        string xml = reader.ReadToEnd();
        // do something with the XML
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • With that code inside a Web API Controller method, I get, "'System.Net.Http.HttpRequestMessage' does not contain a definition for 'InputStream' and no extension method 'InputStream' accepting a first argument of type 'System.Net.Http.HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?)" If the hint is a good one, what need I add? – B. Clay Shannon-B. Crow Raven Feb 24 '14 at 22:50