How to apply an XSLT Stylesheet in C#

前端 未结 4 1657
无人及你
无人及你 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:24

    Based on Daren's excellent answer, note that this code can be shortened significantly by using the appropriate XslCompiledTransform.Transform overload:

    var myXslTrans = new XslCompiledTransform(); 
    myXslTrans.Load("stylesheet.xsl"); 
    myXslTrans.Transform("source.xml", "result.html"); 
    

    (Sorry for posing this as an answer, but the code block support in comments is rather limited.)

    In VB.NET, you don't even need a variable:

    With New XslCompiledTransform()
        .Load("stylesheet.xsl")
        .Transform("source.xml", "result.html")
    End With
    
    0 讨论(0)
  • 2020-11-22 08:30

    This might help you

    public static string TransformDocument(string doc, string stylesheetPath)
    {
        Func<string,XmlDocument> GetXmlDocument = (xmlContent) =>
         {
             XmlDocument xmlDocument = new XmlDocument();
             xmlDocument.LoadXml(xmlContent);
             return xmlDocument;
         };
    
        try
        {
            var document = GetXmlDocument(doc);
            var style = GetXmlDocument(File.ReadAllText(stylesheetPath));
    
            System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();
            transform.Load(style); // compiled stylesheet
            System.IO.StringWriter writer = new System.IO.StringWriter();
            XmlReader xmlReadB = new XmlTextReader(new StringReader(document.DocumentElement.OuterXml));
            transform.Transform(xmlReadB, null, writer);
            return writer.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    
    }   
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 08:47

    Here is a tutorial about how to do XSL Transformations in C# on MSDN:

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

    and here how to write files:

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

    just as a side note: if you want to do validation too here is another tutorial (for DTD, XDR, and XSD (=Schema)):

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

    i added this just to provide some more information.

    0 讨论(0)
提交回复
热议问题