Come applicare un foglio di stile XSLT in C #


Risposte:


177

Ho trovato una possibile risposta qui: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

Dall'articolo:

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

Modificare:

Ma il mio fidato compilatore dice che XslTransformè obsoleto: utilizzare XslCompiledTransforminvece:

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

Da quando ho preso alcune delle tue risposte per creare la classe a cui mi sto collegando, ho pensato di metterlo qui come commento. Spero che semplifichi le cose per le persone: dftr.ca/?p=318
— DFTR

Preferisco questa soluzione invece della versione sovraccarica perché sei in grado di impostare XmlReaderSettings e XmlWriterSettings utilizzando DTD, schemi, ecc.
— Alina B.

2
Devo farlo in VB.NET (che è il mio linguaggio "offspec", preferisco C #) e la tua risposta ha portato alla mia soluzione. Grazie
— Eon

137

Sulla base dell'ottima risposta di Daren, si noti che questo codice può essere abbreviato in modo significativo utilizzando il sovraccarico XslCompiledTransform.Transform appropriato :

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

(Ci scusiamo per aver proposto questo come risposta, ma il code blocksupporto nei commenti è piuttosto limitato.)

In VB.NET, non hai nemmeno bisogno di una variabile:

With New XslCompiledTransform()
    .Load("stylesheet.xsl")
    .Transform("source.xml", "result.html")
End With

16

Ecco un tutorial su come eseguire le trasformazioni XSL in C # su MSDN:

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

e qui come scrivere i file:

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

solo come nota a margine: se vuoi fare anche la validazione ecco un altro tutorial (per DTD, XDR e XSD (= Schema)):

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

l'ho aggiunto solo per fornire qualche informazione in più.


6
Questa è una risposta solo link. Si prega di includere le parti pertinenti delle pagine collegate.
— Thomas Weller,

1

Questo potrebbe aiutarti

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;
    }

}   
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.