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

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

By , 18 May 2005
 

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
Architect
Australia Australia
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMd. Humayun Rashed21 Mar '13 - 3:10 
QuestionGreat, thank youmemberRoger Taylor5 Nov '12 - 1:04 
QuestionGreat, thanksmemberRoger Taylor5 Nov '12 - 1:04 
GeneralRedirect to the pagememberhichamveo10 Feb '10 - 22:36 
GeneralMy vote of 1memberReza Qorbani23 Jan '10 - 13:56 
GeneralRe: My vote of 1memberPinhead_Me10 Mar '11 - 14:31 
GeneralThanksmembertheonlyremedy5 Nov '09 - 1:14 
GeneralThanksmemberredware.com2 Nov '09 - 0:09 
GeneralNicemembersashidhar8 Oct '09 - 19:49 
Nice Work Dude ..!
 
MyFirstArticlePublished: MenuControlSelectedItem
Why Do Some People Forget To Mark as Answer .If It Helps.

GeneralCode pleasememberFanie23 Sep '09 - 1:17 

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

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