Friday, June 5, 2009

Display XML string on Browser

Initially I thought that it is very simple task. I was surprised myself that it is not so simple as I thought.

My requirement: XMLs are stored in SQL Server 2005 database in XML data type field. On web page, I wanted to display a hyperlink and when user click to this hyperlink, then the xml should display for the selected ID.

My Solution:
Created a sample web application, here is the code snippet of Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ShowXmlOnPage._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd >">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"></a>;
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>" ><head runat="server"> <title>Untitled Page</title></head><body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>

And then subsequent code behind page(Default.aspx.cs) is:


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Xsl;

namespace ShowXmlOnPage
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(MapPath("Vendor.xml"));
Response.Write(LoadXmldocument(doc));
}
private string LoadXmldocument(XmlDocument xmlDocument)
{
System.IO.Stream s = System.IO.File.OpenRead(MapPath("defaultss.xsl"));
System.Xml.XmlReader reader = System.Xml.XmlReader.Create(s);
XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(reader);
System.Text.StringBuilder sb = new System.Text.StringBuilder();
XmlWriter writer = XmlWriter.Create(sb);
transform.Transform(xmlDocument, writer);
return sb.ToString();
}
}
}

The .NET Solution file require a couple of other files too.
1. Vendor.xml file - Here is the XML file:
<?xml version="1.0" encoding="utf-16" ?><Response> <Success> <Operation>PlaceOrderCredit,PlaceSalesOrderPOs,Payment</Operation> <SORefNumber /> <PONumber /> <PONumber /> <SalesReturnRefNumber /> <CreditRefNumber /> <RMARefNumber /> <PaymentRefNumber /> </Success> <Failure> <Operation>PlaceOrderCredit,PlaceSalesOrderPOs,Payment</Operation> <SORefNumber /> <PONumber /> <PONumber /> <SalesReturnRefNumber /> <CreditRefNumber /> <RMARefNumber /> <PaymentRefNumber /> <ErrorMessage /> </Failure></Response>

2. defaultss.xsl - This file I downloaded from "http://blorgh.files.wordpress.com/2006/10/defaultss.txt". Thanks to the author who has modified this xsl file. Save the file as "defaultss.xsl" - that is what I referenced in the code.

And that's it. This is the sample application where Vendor.xml is physically present in the solution folder. It is required to have defaultss.xsl present in the web folder too.

Happy Coding!!!