Domanda

Il seguente codice funziona e prende XSL e XML dal disco locale e restituisce l'XML trasformato sulla variabile XTRANSOUTUTUT.

Dim XmlInputPath As String = "C:\Any.XML"
Dim XslInputPath As String = "C:\Any.XSL"

Dim StringWriter As New System.IO.StringWriter
Dim XsltTransformation As New XslCompiledTransform(True)
Dim XsltArgumentList As New XsltArgumentList
Dim Xtransoutput As String = Nothing

XsltTransformation.Load(XslInputPath)
XsltTransformation.Transform(XmlInputPath, XsltArgumentList, StringWriter)
Xtransoutput = StringWriter.ToString
.

Il mio problema è che ho sia XML che XSL in stringhe separate già, non sono sul disco e non posso scriverli su disco per motivi di sicurezza.Eventuali suggerimenti su come ottenerli per lavorare da stringhe piuttosto che dai file del disco?

Tia!

È stato utile?

Soluzione

Ecco un esempio di C # - Convertendolo in VB è lasciato come esercizio al lettore :))

using System;
using System.IO;
using System.Xml;
using System.Xml.Xsl;

namespace XsltInMemory
{
    class XsltInMemory
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XslCompiledTransform xslt = new XslCompiledTransform();

            doc.LoadXml("<t/>");

            StringReader sr = new StringReader(

@"<xsl:stylesheet version='1.0'
 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
 <xsl:output omit-xml-declaration='yes' indent='yes'/>

 <xsl:template match='node()|@*'>
  <xsl:copy>
   <xsl:apply-templates select='node()|@*'/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>"

            );

            MemoryStream ms = new MemoryStream();

            xslt.Load(new XmlTextReader(sr));

            xslt.Transform(doc, null, ms);

            ms.Flush();
            ms.Position = 0;

            StreamReader sr2 = new StreamReader(ms);

            Console.Write(sr2.ReadToEnd());
        }
    }
}
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top