Pergunta

O código a seguir funciona e pega o XSL e XML do disco local e retorna o XML transformado para a variável Xtransoutput.

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

Meu problema é que já tenho o XML e o XSL em strings separadas, eles não estão no disco e não posso gravá-los no disco por motivos de segurança.Alguma sugestão sobre como fazer com que funcionem a partir de strings em vez de arquivos de disco?

TIA!

Foi útil?

Solução

Aqui está um exemplo em C# -- convertê-lo para VB é deixado como exercício para o leitor :))

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());
        }
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top