Mental Jetsam

By Peter Finch

How to Process a HTTP XML Post in C#

Posted by pcfinch on June 15, 2007

This is a quick bit of code that can be used to read and decode XML data submitted to a C# HTTP web service (usually derived from a IHttpHandler) using the POST method. This is better that a GET as more data can be submitted and the data can be structured. In this example the incoming XML on the POST is…


Reading the XML data is really easy using the System.Xml API.

StreamReader reader = new StreamReader(
    context.Request.InputStream, System.Text.Encoding.UTF8);
String sXMLRequest = reader.ReadToEnd();
XmlDocument xmlRequest = new XmlDocument();

Once the XML has been loaded to a DOM, elements, attributes and text from the DOM can easily be extracted using the SelectSingleNode() and SelectNodes() members functions available on each node. Both functions take an XPath expression as input. This makes these two functions very powerful when processing the DOM.

XmlNode xmlUserId = xmlRequest.SelectSingleNode(
String sUserId = (xmlUserId != null) ?
    xmlUserId.InnerText : "undefined" ;
context.Response.ContentType = "text/html";
context.Response.Write("<br>UserId = " +

XmlNodeList xmlParts = xmlRequest.SelectNodes(
if (xmlParts != null) {
    foreach (XmlNode node in xmlParts) {
        context.Response.Write("<br>Add part to catalog : " +

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: