Click here to Skip to main content
6,936,634 members and growing! (18,672 online)
Email Password   helpLost your password?
 
Platforms, Frameworks & Libraries » .NET Framework » General     Intermediate License: The Code Project Open License (CPOL)

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

By S Sansanwal

This article describes the function to post XML data to an ASP.NET page and then read the data on an ASP.NET page
C#, Windows, .NET, ASP.NET, Visual-Studio, IIS5.1, IIS6, Architect, DBA, Dev, QA
Posted:18 May 2005
Views:126,008
Bookmarked:43 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 4.80 Rating: 3.53 out of 5
5 votes, 21.7%
1

2

3
5 votes, 21.7%
4
13 votes, 56.5%
5

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
  /// <summary>
  /// Read XML data from file
  /// </summary>
  /// <param name="file"></param>
  /// <returns>returns file content in XML string format</returns>
  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)

About the Author

S Sansanwal


Member

Occupation: Architect
Location: Australia Australia

Other popular .NET Framework articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralRedirect to the page Pinmemberhichamveo23:36 10 Feb '10  
GeneralMy vote of 1 PinmemberReza Qorbani14:56 23 Jan '10  
GeneralThanks Pinmembertheonlyremedy2:14 5 Nov '09  
GeneralThanks Pinmemberredware.com1:09 2 Nov '09  
GeneralNice Pinmembersashidhar20:49 8 Oct '09  
GeneralCode please PinmemberFanie2:17 23 Sep '09  
GeneralRe: Code please PinmemberS Sansanwal20:10 23 Sep '09  
GeneralThanks for the code PinmemberSarif Bin Ishak23:11 28 Jul '09  
Generalhow to post a string to an URL Pinmembernaveen_bij23:15 23 Dec '08  
GeneralHow to get the values from the database into a html page using C#.Net Pinmembersatyaanand.andra@gmail.com21:52 29 Jun '08  
GeneralRe: How to get the values from the database into a html page using C#.Net PinmemberS Sansanwal1:44 30 Jun '08  
GeneralRe: How to get the values from the database into a html page using C#.Net Pinmembersatyaanand.andra@gmail.com3:11 30 Jun '08  
GeneralTrouble with the server side code? PinmemberSamsingh10:39 26 Jun '08  
QuestionBy the way, this used to work under IIS 5, but now under 6 it doesn't anymore. Pinmemberepastorejr1:21 2 Nov '06  
GeneralWebRequest and WebResponse are in System.Net namespace Pinmemberglavonjahans_chrstn11:48 30 Sep '06  
QuestionCould you post all of the code? Pinmemberplatorres10:41 13 Sep '06  
GeneralIt doesn't work for me Pinmembermarco315:41 1 Jun '06  
GeneralThis is a quick and easy guide!! Pinmemberideasfreelance9:03 10 Nov '05  
GeneralNo writeup Pinmembervikramk14:24 1 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 18 May 2005
Editor: Deeksha Shenoy
Copyright 2005 by S Sansanwal
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project