Francesco Geri

Il blog di Francesco Geri
posts - 52, comments - 47, trackbacks - 1

My Links

News



Anch'io metto nel mio blog cose che scrivo così, tanto per fare, tanto per condividere miei appunti, senza prendermi la briga di garantirne l'infallibilità, né l'assoluta correttezza, senza pretese e con grande umilté.

Quanti mi hanno visto dal 25/10/2007:
...dettagli

Gli sponsore...
banner per la sponsorizzazione
Racconti Urbinati

Archives

Post Categories

Altre

Blogs

Trasformazione di un file XML in un file HTML tramite un XSLT

La trasformazione via codice di un file XML in HTML tramite file XSLT è molto semplice (cito liberamente da MSDN):

 

' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load("output.xsl")

' Execute the transform and output the results to a file.
xslt.Transform("books.xml", "books.html")

 

Il codice riportato lavora su file fisici, messi da qualche parte.
Se però non si vuole sfruttare il file system, si può usare una funzione come la seguente, che consente di fare la trasformazione senza aver file fisici in gioco.

 

  Function TransformXML(ByVal xDoc As Xml.XmlDocument, ByVal xsltText As String) As String

    ' Carica l'xslt

    Dim xslt As New Xml.Xsl.XslCompiledTransform()

    Dim xsltStringReader As New IO.StringReader(xsltText)

    Dim xsltXmlReader As New Xml.XmlTextReader(xsltStringReader)

    xslt.Load(xsltXmlReader)

    ' Crea il reader per l'XML in XDOC

    Dim xDocStringReader As New IO.StringReader(xDoc.OuterXml)

    Dim xDocXmlReader As New Xml.XmlTextReader(xDocStringReader)

    ' Crea il writer per l'html

    Dim htmlStringBuilder As New Text.StringBuilder()

    Dim htmlStringWriter As New IO.StringWriter(htmlStringBuilder)

    Dim htmlXmlReaderWriter As New Xml.XmlTextWriter(htmlStringWriter)

    ' Esegue la trasformazione

    xslt.Transform(xDocXmlReader, htmlXmlReaderWriter)

    ' Retuisce il contenuto html

    Return htmlStringBuilder.ToString()

  End Function

Print | posted on mercoledì 2 aprile 2008 2.55 | Filed Under [ Tips .Net XML ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 7 and type the answer here:

Powered by: