In this article I will explain how to display XML files in browser in ASP.Net using C# and VB.Net, this question was asked to me by a member on forums How to display xml file in browser using ASP.Net
 
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
Display XML File in Web Browser in ASP.Net using C# and VB.Net
 
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
Display XML File in Web Browser in ASP.Net using C# and VB.Net
 
Mozilla Firefox
Display XML File in Web Browser in ASP.Net using C# and VB.Net
 
Google Chrome
Display XML File in Web Browser in ASP.Net using C# and VB.Net
 
Demo
 
Downloads