Tuesday, August 16, 2011

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();

}

10 comments:

  1. Howdy very nice site!! Man .. Beautiful .. Wonderful .. I'll bookmark your web site and take the feeds also?I am satisfied to seek out numerous helpful information here in the post, we want develop more strategies on this regard, thank you for sharing. . . . . .

    ReplyDelete
  2. 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

    ReplyDelete
  3. Hi there, simply become alert to your blog thru Google, and found that it is really informative. I am gonna be careful for brussels. I?ll be grateful in case you proceed this in future. Many other folks might be benefited from your writing. Cheers!

    ReplyDelete
  4. Excellent goods from you, man. I have take note your stuff previous to and you are simply extremely magnificent. I really like what you've acquired right here, certainly like what you are stating and the way during which you say it. You are making it entertaining and you continue to care for to keep it sensible. I can't wait to read far more from you. This is actually a great web site.

    ReplyDelete
  5. This is very fascinating, You're an excessively professional blogger. I've joined your rss feed and sit up for searching for extra of your excellent post. Also, I have shared your website in my social networks

    ReplyDelete
  6. You actually make it appear so easy with your presentation however I in finding this topic to be actually something that I think I would never understand. It seems too complex and extremely huge for me. I am having a look ahead to your subsequent submit, I?ll attempt to get the grasp of it!

    ReplyDelete
  7. It's perfect time to make a few plans for the future and it is time to be happy. I've read this submit and if I may I desire to counsel you some fascinating things or suggestions. Perhaps you could write next articles relating to this article. I wish to read even more things approximately it!

    ReplyDelete
  8. 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!

    ReplyDelete
  9. 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!

    ReplyDelete
  10. Someone essentially assist to make significantly posts I would state. That is the first time I frequented your web page and up to now? I amazed with the analysis you made to create this particular put up extraordinary. Fantastic process!

    ReplyDelete