vb 2010 XslCompiledTransform need to Transform from string instead of from URI disk file

1.4k Views Asked by At

The following code works and takes the XSL and XML from local disk and returns the transformed XML to the 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

My problem is that I have both the XML and the XSL in separate strings already, they are not on the disk and I don't can't write them to disk for security reasons. Any suggestions on how to get these to work from strings rather than from disk files?

TIA!

1

There are 1 best solutions below

0
On BEST ANSWER

Here is a C# example -- converting it to VB is left as exercise to the reader :) )

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