XML POST and parse in ASP.NET

2020-04-08 12:31发布

问题:

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?

回答1:

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
    }
}


标签: c# asp.net xml