Thursday, October 14, 2010

XML to HTML transformation using Xslt

Namespace Required:

1. System.Xml
2. System.Xml.XPath
3. System.Xml.Xsl

public static void Transform(string sXmlPath, string sXslPath)

{

try

{

// Step 1: load the Xml doc

XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;

XslTransform myXslTrans = new XslTransform() ;



// Step 2: load the Xsl

myXslTrans.Load(sXslPath) ;



// Step 3: create the output stream



XmlTextWriter myWriter = new XmlTextWriter("result.html", null);



// Step 4: do the actual transform of Xml





myXslTrans.Transform(myXPathDoc,null, myWriter);

myWriter.Close() ;

}

catch(Exception e)

{

Console.WriteLine("Exception: {0}", e.ToString());

}

}

No comments: