下面的例子会用到一个名为doc的XPathDocument类的实例(其中加载了需要转换的XML文档)和一个名为trans的XslTransform类的实例(负责执行从XML到HTML的转换)。转换的结果会被转化成一个字符串类型,而且为了显示和比较的目的,它将被写进一个TextBox Web Server控件的Text属性中去。生成XPathDocument类和XslTransform类的实例代码如下所示:XPathDocument doc = new XPathDocument(Server.MapPath ("XML/Customers.xml")); XslTransform trans = new XslTransform(); trans.Load(Server.MapPath("XSLT/Customers.xslt"));
以下是一个将StringWriter类和XslTransform类结合使用的例子://************ StringReader XSLT Output Capture StringWriter sw = new StringWriter(); trans.Transform(doc,null,sw); /* A StringBuilder class is automatically written to by the StringWriter class. To get the value in the StringBuilder you can use the ToString() method as shown below */ this.txtStringBuilder.Text = sw.ToString(); sw.Close();