Pregunta

El siguiente código funciona y toma el disco XSL y XML desde el disco local y devuelve el XML transformado a la variable 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

Mi problema es que ya tengo la XML como el XSL en cadenas separadas, no están en el disco y no puedo escribirlas en el disco por razones de seguridad.¿Alguna sugerencia sobre cómo hacer que estos trabajen desde cadenas en lugar de de archivos de disco?

tia!

¿Fue útil?

Solución

Aquí hay un ejemplo C # - convertirlo a VB se deja como ejercicio al lector :))

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 bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top