Click here to Skip to main content
11,715,022 members (68,069 online)
Click here to Skip to main content

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

, 18 May 2005 CPOL 276.7K 61
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;
 try
 {
  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
  writer.WriteLine(this.GetTextFromXMLFile(fileName));
  writer.Close();
  // Send the data to the webserver
  rsp = req.GetResponse();

 }
 catch(WebException webEx)
 {

 }
 catch(Exception ex)
 {

 }
 finally
 {
  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();
 reader.Close();
 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(); 
}

License

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

Share

About the Author

S Sansanwal
Architect
Australia Australia
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Md. Humayun Rashed21-Mar-13 3:10
memberMd. Humayun Rashed21-Mar-13 3:10 
QuestionGreat, thank you Pin
Roger Taylor5-Nov-12 1:04
memberRoger Taylor5-Nov-12 1:04 
QuestionGreat, thanks Pin
Roger Taylor5-Nov-12 1:04
memberRoger Taylor5-Nov-12 1:04 
GeneralRedirect to the page Pin
hichamveo10-Feb-10 22:36
memberhichamveo10-Feb-10 22:36 
GeneralMy vote of 1 Pin
Reza Qorbani23-Jan-10 13:56
memberReza Qorbani23-Jan-10 13:56 
GeneralRe: My vote of 1 Pin
Pinhead_Me10-Mar-11 14:31
memberPinhead_Me10-Mar-11 14:31 
GeneralThanks Pin
theonlyremedy5-Nov-09 1:14
membertheonlyremedy5-Nov-09 1:14 
GeneralThanks Pin
redware.com2-Nov-09 0:09
memberredware.com2-Nov-09 0:09 
GeneralNice Pin
sashidhar8-Oct-09 19:49
membersashidhar8-Oct-09 19:49 
GeneralCode please Pin
Fanie23-Sep-09 1:17
memberFanie23-Sep-09 1:17 
GeneralRe: Code please Pin
S Sansanwal23-Sep-09 19:10
memberS Sansanwal23-Sep-09 19:10 
GeneralThanks for the code Pin
Sarif Bin Ishak28-Jul-09 22:11
memberSarif Bin Ishak28-Jul-09 22:11 
Questionhow to post a string to an URL Pin
naveen_bij23-Dec-08 22:15
membernaveen_bij23-Dec-08 22:15 
QuestionHow to get the values from the database into a html page using C#.Net Pin
satyaanand.andra@gmail.com29-Jun-08 20:52
membersatyaanand.andra@gmail.com29-Jun-08 20:52 
AnswerRe: How to get the values from the database into a html page using C#.Net Pin
S Sansanwal30-Jun-08 0:44
memberS Sansanwal30-Jun-08 0:44 
Hi Anand,

Why don't you generate HTML file dynamically in your .NET code or use AJAX.

Cheers,
Sanjay


GeneralRe: How to get the values from the database into a html page using C#.Net Pin
satyaanand.andra@gmail.com30-Jun-08 2:11
membersatyaanand.andra@gmail.com30-Jun-08 2:11 
QuestionTrouble with the server side code? Pin
Samsingh26-Jun-08 9:39
memberSamsingh26-Jun-08 9:39 
QuestionBy the way, this used to work under IIS 5, but now under 6 it doesn't anymore. Pin
epastorejr2-Nov-06 0:21
memberepastorejr2-Nov-06 0:21 
GeneralWebRequest and WebResponse are in System.Net namespace Pin
glavonjahans_chrstn30-Sep-06 10:48
memberglavonjahans_chrstn30-Sep-06 10:48 
QuestionCould you post all of the code? Pin
platorres13-Sep-06 9:41
memberplatorres13-Sep-06 9:41 
GeneralIt doesn't work for me Pin
marco311-Jun-06 4:41
membermarco311-Jun-06 4:41 
GeneralThis is a quick and easy guide!! Pin
ideasfreelance10-Nov-05 8:03
memberideasfreelance10-Nov-05 8:03 
GeneralNo writeup Pin
vikramk1-Oct-05 13:24
membervikramk1-Oct-05 13: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.150901.1 | Last Updated 18 May 2005
Article Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid