如何在C#中应用XSLT样式表

时间:2020-03-05 18:44:53  来源:igfitidea点击:

我想使用Cand将XSLT样式表应用于XML文档,并将输出写入文件。

解决方案

回答

我在这里找到了可能的答案:http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

从文章:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;

编辑:

但是我可信赖的编译器说,XslTransform已经过时了:改用XslCompiledTransform

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);

回答

这是有关如何在Con MSDN中进行XSL转换的教程:

http://support.microsoft.com/kb/307322/en-us/

这里是如何写文件:

http://support.microsoft.com/kb/816149/en-us

只是作为一个旁注:如果我们也想进行验证,这是另一个教程(针对DTD,XDR和XSD(= Schema)):

http://support.microsoft.com/kb/307379/en-us/

我添加此只是为了提供更多信息。