For this article I have created one XML file using the Customer records from the Northwind database, this file will be displayed in browser on the click of the Button
HTML Markup
The HTML Markup consist of a Button which when clicked will display the XML file in browser
<asp:Button ID="btnViewXML" runat="server" Text="View XML" OnClick="ViewXML" />
Displaying the XML File in Browser
When the Button is clicked the XML is loaded using its file path and its content written to the Response Stream after setting the Content Type (MIME Type) as application/xml.
C#
protected void ViewXML(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/xml";
Response.WriteFile(Server.MapPath("~/Customers.xml"));
Response.Flush();
Response.End();
}
VB.Net
Protected Sub ViewXML(sender As Object, e As EventArgs)
Response.Clear()
Response.Buffer = True
Response.Charset = ""
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.ContentType = "application/xml"
Response.WriteFile(Server.MapPath("~/Customers.xml"))
Response.Flush()
Response.[End]()
End Sub
The following screenshots displays the XML file being rendered in Internet Explorer, Mozilla Firefox and Google Chrome browsers
Internet Explorer
Mozilla Firefox
Google Chrome
Demo
Downloads