Click here to Skip to main content
Click here to Skip to main content

Post XML Data to an ASP.NET Page using C#

, 18 May 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes the function to post XML data to an ASP.NET page and then read the data on an ASP.NET page

Code For Posting

To post XML, use the following function:

   WebRequest req = null;
   WebResponse rsp = null;
    string fileName = "C:\test.xml";
    string uri = "http://localhost/PostXml/Default.aspx";
    req = WebRequest.Create(uri);
    //req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
    req.Method = "POST";        // Post method
    req.ContentType = "text/xml";     // content type
    // Wrap the request stream with a text-based writer
    StreamWriter writer = new StreamWriter(req.GetRequestStream());
    // Write the XML text into the stream
    // Send the data to the webserver
    rsp = req.GetResponse();
   catch(WebException webEx)
   catch(Exception ex)
    if(req != null) req.GetRequestStream().Close();
    if(rsp != null) rsp.GetResponseStream().Close();
   }Function to read xml data from local system
  /// <span class="code-SummaryComment"><summary></span>
  /// Read XML data from file
  /// <span class="code-SummaryComment"></summary></span>
  /// <span class="code-SummaryComment"><param name="file"></param></span>
  /// <span class="code-SummaryComment"><returns>returns file content in XML string format</returns></span>
  private string GetTextFromXMLFile(string file)
   StreamReader reader = new StreamReader(file);
   string ret = reader.ReadToEnd();
   return ret;

Code For Reading Posted Data

Now, on the Web server in the ASP.NET page, write the following code to access the posted data:

private void Page_Load(object sender, EventArgs e)
     page.Response.ContentType = "text/xml";
    // Read XML posted via HTTP
    StreamReader reader = new StreamReader(page.Request.InputStream);
    String xmlData = reader.ReadToEnd(); 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

S Sansanwal
Australia Australia
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberMd. Humayun Rashed21-Mar-13 4:10 
QuestionGreat, thank you PinmemberRoger Taylor5-Nov-12 2:04 
Great, thank you
QuestionGreat, thanks PinmemberRoger Taylor5-Nov-12 2:04 
GeneralRedirect to the page Pinmemberhichamveo10-Feb-10 23:36 
GeneralMy vote of 1 PinmemberReza Qorbani23-Jan-10 14:56 
GeneralRe: My vote of 1 PinmemberPinhead_Me10-Mar-11 15:31 
GeneralThanks Pinmembertheonlyremedy5-Nov-09 2:14 
GeneralThanks Pinmemberredware.com2-Nov-09 1:09 
GeneralNice Pinmembersashidhar8-Oct-09 20:49 
GeneralCode please PinmemberFanie23-Sep-09 2:17 
GeneralRe: Code please PinmemberS Sansanwal23-Sep-09 20:10 
GeneralThanks for the code PinmemberSarif Bin Ishak28-Jul-09 23:11 
Questionhow to post a string to an URL Pinmembernaveen_bij23-Dec-08 23:15 
QuestionHow to get the values from the database into a html page using C#.Net Pinmembersatyaanand.andra@gmail.com29-Jun-08 21:52 
AnswerRe: How to get the values from the database into a html page using C#.Net PinmemberS Sansanwal30-Jun-08 1:44 
GeneralRe: How to get the values from the database into a html page using C#.Net Pinmembersatyaanand.andra@gmail.com30-Jun-08 3:11 
QuestionTrouble with the server side code? PinmemberSamsingh26-Jun-08 10:39 
QuestionBy the way, this used to work under IIS 5, but now under 6 it doesn't anymore. Pinmemberepastorejr2-Nov-06 1:21 
GeneralWebRequest and WebResponse are in System.Net namespace Pinmemberglavonjahans_chrstn30-Sep-06 11:48 
QuestionCould you post all of the code? Pinmemberplatorres13-Sep-06 10:41 
GeneralIt doesn't work for me Pinmembermarco311-Jun-06 5:41 
GeneralThis is a quick and easy guide!! Pinmemberideasfreelance10-Nov-05 9:03 
GeneralNo writeup Pinmembervikramk1-Oct-05 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 18 May 2005
Article Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid