How to apply an XSLT Stylesheet in C#

前端 未结 4 1735
无人及你
无人及你 2020-11-22 07:53

I want to apply an XSLT Stylesheet to an XML Document using C# and write the output to a File.

4条回答
  •  我在风中等你
    2020-11-22 08:35

    I found a possible answer here: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

    From the article:

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

    Edit:

    But my trusty compiler says, XslTransform is obsolete: Use XslCompiledTransform instead:

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

提交回复
热议问题