|
HTML clipboard
XPathDocument myXPathDoc = new XPathDocument(<xml file path>) ;
2) Load the Xsl file
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(<xsl file path>);
3) Create a stream for output
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
4) Perform the actual transformation
myXslTrans.Transform(myXPathDoc,null,myWriter) ;
Sample Code
Check the code below for a C# command line application that transforms Xml with
Xsl.
Compile and run the application. Usage is as follows:
XmlTransformUtil.exe <xml file path> <xsl file path>
You could use the provided sampledoc.xml and sample.xsl to have a go at it.
You should see a 'result.html' in the same folder as the application with the
results of the transformation.
using System ;
using System.IO ;
using System.Xml ;
using System.Xml.Xsl ;
using System.Xml.XPath ;
public class XmlTransformUtil{
public static void Main(string[] args){
if (args.Length
== 2){
Transform(args[0], args[1]) ;
}else{
PrintUsage()
;
}
}
public static void Transform(string
sXmlPath, string sXslPath){
try{
XPathDocument
myXPathDoc = new XPathDocument(sXmlPath) ;
XslTransform
myXslTrans = new XslTransform() ;
myXslTrans.Load(sXslPath) ;
XmlTextWriter
myWriter = new XmlTextWriter
("result.html", null);
myXslTrans.Transform(myXPathDoc,null, myWriter);
myWriter.Close() ;
}catch(Exception
e){
Console.WriteLine("Exception: {0}", e.ToString());
}
}
public static void PrintUsage(){
Console.WriteLine
("Usage: XmlTransformUtil.exe <xml path> <xsl path>");
}
}
|