Home > ASP.Net, C# > Exporting XML in a C# ASP .NET Web Application

Exporting XML in a C# ASP .NET Web Application

XML (extensible markup language) is a popular format of data for importing and exporting between different applications designed using different programming languages. Since XML uses a standardized format of data, applications can easily parse the XML data to pull out specific fields, blocks, and even write their own XML files. XML is especially useful as a protocol for communicating over the Internet with applications

using (System.IO.MemoryStream stream = new System.IO.MemoryStream())

{

// Create an XML document. Write our specific values into the document.

System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stream, System.Text.Encoding.ASCII);

// Write the XML document header.

xmlWriter.WriteStartDocument();

// Write our first XML header.

xmlWriter.WriteStartElement(“WebApplications”);

// Write an element representing a single web application object.

xmlWriter.WriteStartElement(“WebApplication”);

// Write child element data for our web application object.

xmlWriter.WriteElementString(“Date”, DateTime.Now.ToString());

xmlWriter.WriteElementString(“Programmer”, “Primary Objects”);

xmlWriter.WriteElementString(“Name”, “Hello World”);

xmlWriter.WriteElementString(“Language”, “C# ASP .NET”);

xmlWriter.WriteElementString(“Status”, “Complete”);

// End the element WebApplication

xmlWriter.WriteEndElement();

// End the document WebApplications

xmlWriter.WriteEndElement();

// Finalize the XML document by writing any required closing tag.

xmlWriter.WriteEndDocument();

// To be safe, flush the document to the memory stream.

xmlWriter.Flush();

// Convert the memory stream to an array of bytes.

byte[] byteArray = stream.ToArray();

// Send the XML file to the web browser for download.

Response.Clear();

Response.AppendHeader(“Content-Disposition”, “filename=MyExportedFile.xml”);

Response.AppendHeader(“Content-Length”, byteArray.Length.ToString());

Response.ContentType = “application/octet-stream”;

Response.BinaryWrite(byteArray);

xmlWriter.Close();

}

Advertisements
Categories: ASP.Net, C#
  1. Iplayer Abroad
    September 14, 2011 at 3:03 am

    That is very attention-grabbing, You are an overly skilled blogger. I’ve joined your rss feed and stay up for seeking more of your excellent post. Additionally, I’ve shared your website in my social networks

  2. Hexenkostüm
    September 15, 2011 at 11:37 am

    Hello there, simply turned into aware of your blog thru Google, and found that it is really informative. I?m gonna watch out for brussels. I will appreciate if you continue this in future. A lot of other folks will be benefited from your writing. Cheers!

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: