Windows Phone Developers

Saturday, October 11, 2008

Convert an XML file to HTML file using C#

Convert an XML file to HTML file using XSLT

Data is best stored in XML format, but the presentations are better in HTML. Here is a case where the following data in a XML file needs to be converted to a HTML file


Here is the XML File for conversion

< ?xml version="1.0" encoding="utf-8"? >

< !-- Sample File For Dot Net Tips & Tricks-- >

< !-- http://dotnetdud.blogspot.com -- >

< invoice InvNo="DND1232" InvDate="3-Oct-2008" >

< customer >

< custID >CID134< /custID >

< custName >Allirajan Ramachandran< /custName >

< /customer >

< items >

< item >

< MovieID >F123455< /MovieID >

< MovieName >Happy Days< /MovieName >

< RentedDate >3-Oct-2008< /RentedDate >

< ReturnDate >5-Oct-2008< /ReturnDate >

< /item >

< item >

< MovieID >F123462< /MovieID >

< MovieName >Lord Of Rings< /MovieName >

< RentedDate >3-Oct-2008< /RentedDate >

< ReturnDate >5-Oct-2008< /ReturnDate >

< /item >

< item >

< MovieID >F123232< /MovieID >

< MovieName >Drona< /MovieName >

< RentedDate >1-Oct-2008< /RentedDate >

< ReturnDate >3-Oct-2008< /ReturnDate >

< /item >

< /items >

< /invoice >

The XML file should be transformed to HTML using XSLT. This needs a stylesheet (XSL) defining what needs to be transformed and how it needs to be done.

Here is a sample stylesheet

< ?xml version="1.0" encoding="utf-8"? >

< !-- Om Ganeshaya Namaha-- >

< !-- Om Satguru Seshadri Swamigal Thiruvadike-- >

< !-- Om Varahi Namaha-- >

< !-- Om Saravana Bhava-- >

< xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"

xmlns:vbadud="http://vbadud.in"

>

< msxsl:script implements-prefix="vbadud" language ="C#" >

< ![CDATA[

public string Defaulted(String RetDate)

{

if (DateTime.Parse(RetDate) < DateTime.Now)

{

return "Defaulted";

}

else

{

return "--";

}

}

public string Today()

{

return DateTime.Now.ToString();

}

]] >

< /msxsl:script >

< xsl:output method="html" indent="yes"/ >

< xsl:template match="invoice" >

< h1 >DND Video Library - Invoice< /h1 >

< p >

Invoice Date : =

< xsl:value-of select ="vbadud:Today()"/ >

< /p >

< p >

Invoice No : =

< xsl:value-of select ="@InvNo"/ >

< /p >

< xsl:apply-templates/ >

< /xsl:template >

< xsl:template match ="customer" >

=========== Customer Information =========

< br/ >

Customer ID :=

< xsl:value-of select ="custID"/ >

< br/ >

Customer Name :=

< xsl:value-of select ="custName"/ >

< br/ >

============================================

< br/ >

< xsl:apply-templates/ >

< /xsl:template >

< xsl:template match ="items" >

< table >

< th >Movie ID< /th >

< th >Movie Name< /th >

< th >Rented Date< /th >

< th >Return Date< /th >

< th >Defaulted< /th >

< xsl:apply-templates/ >

< /table >

< /xsl:template >

< xsl:template match ="item" >

< tr >

< td >< xsl:value-of select ="MovieID"/ > < /td >

< td >

< xsl:value-of select ="MovieName"/ >

< /td >

< td >

< xsl:value-of select ="RentedDate"/ >

< /td >

< td >

< xsl:value-of select ="ReturnDate"/ >

< /td >

< td >

< xsl:value-of select ="vbadud:Defaulted(ReturnDate)"/ >

< /td >

< /tr >

< /xsl:template >

< /xsl:stylesheet >

The above stylesheet uses C# scripting to identify defaulters

The following C# code will transform (convert) the XML to HTML

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml;

using System.Xml.XPath;

using System.Xml.Xsl;

public void transform_Xml2Html()

{

XslCompiledTransform XSLT = new XslCompiledTransform();

XsltSettings settings = new XsltSettings();

settings.EnableScript = true ;

XSLT.Load(@"c:\shasurdata\forblogger\VideoLibXSLTFile.xslt", settings, new XmlUrlResolver() );

XSLT.Transform(XMLFile ,@"c:\shasurdata\forblogger\SalesInvoice.html");

}

settings.EnableScript = true enables scripting in XSL file. The code uses XslCompiledTransform methods to convert the XML files

Simple isn’t it. Here you have the output:

HTML Source

< h1 xmlns:vbadud="http://vbadud.in" >DND Video Library - Invoice< /h1 >

< p xmlns:vbadud="http://vbadud.in" >

Invoice Date : =

04-10-2008 20:37:02< /p >

< p xmlns:vbadud="http://vbadud.in" >

Invoice No : =

DND1232< /p >

=========== Customer Information =========

< br xmlns:vbadud="http://vbadud.in" >

Customer ID :=

CID134< br xmlns:vbadud="http://vbadud.in" >

Customer Name :=

Allirajan Ramachandran< br xmlns:vbadud="http://vbadud.in" >

============================================

< br xmlns:vbadud="http://vbadud.in" >

CID134

Allirajan Ramachandran

< table xmlns:vbadud="http://vbadud.in" >

< th >Movie ID< /th >

< th >Movie Name< /th >

< th >Rented Date< /th >

< th >Return Date< /th >

< th >Defaulted< /th >

< tr >

< td >F123455< /td >

< td >Happy Days< /td >

< td >3-Oct-2008< /td >

< td >5-Oct-2008< /td >

< td >--< /td >

< /tr >

< tr >

< td >F123462< /td >

< td >Lord Of Rings< /td >

< td >3-Oct-2008< /td >

< td >5-Oct-2008< /td >

< td >--< /td >

< /tr >

< tr >

< td >F123232< /td >

< td >Drona< /td >

< td >1-Oct-2008< /td >

< td >3-Oct-2008< /td >

< td >Defaulted< /td >

< /tr >

< /table >


And here is the HTML output





Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

System.Xml.Xsl.XslTransformException was unhandled - Message="Execution of scripts was prohibited. Use the XsltSettings.EnableScript property to ena

System.Xml.Xsl.XslTransformException was unhandled - Message="Execution of scripts was prohibited. Use the XsltSettings.EnableScript property to enable it. An error occurred at "

private void TransformXML()

{

XslCompiledTransform trans = new XslCompiledTransform();

XsltSettings xslt_set = new XsltSettings();

xslt_set.EnableScript = true;

trans.Load(@"C:\ShasurData\Sample.xslt",xslt_set, new XmlUrlResolver() );

//XsltSettings.TrustedXslt

trans.Transform(@"C:\ShasurData\ForBlogger\Sample.xml", @"C:\ShasurData\ForBlogger\TransformedSample.html");

}

Solution

xslt_set.EnableScript = true;

and pass the xsltsettings to the load method

trans.Load(@"C:\ShasurData\Sample.xslt",xslt_set, new XmlUrlResolver() );





Enable Scripting Error in XSLT Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

Method < MethodName > of extension object < NameSpace > cannot be called because it is not public.


This error occurs in XSLT Processing when the function in the msxsl script is not declared as public

< msxsl:script implements-prefix ="vbadud" language="C#" >

< ![CDATA[

decimal TotalAmount(int Qty, decimal Price)

{

return Qty* Price;

}

]] >

< /msxsl:script >

Solution :

Declare the TotalAmount as public

< msxsl:script implements-prefix ="vbadud" language="C#" >

< ![CDATA[

public decimal TotalAmount(int Qty, decimal Price)

{

return Qty* Price;

}

]] >

< /msxsl:script >


Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

What is an Abstract Class / Abstract Classes in C#

Abstract classes are the ones that do not require instantiation. Abstract classes should never be instantiated, it is important to correctly define their constructors. It is also important to ensure that the functionality of your abstract class is correct and easily extended

Abstract (MustInherit in Visual Basic) classes can behave like interfaces or regular classes in that they can define members, and they can provide implementation details but are not required to do so. If an abstract class does not provide implementation details, concrete classes that inherit from the abstract class are required to provide the implementation.

For more info please visit msdn.microsoft.com

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon