2

以下代码有效,从本地磁盘获取 XSL 和 XML,并将转换后的 XML 返回到变量 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

我的问题是我已经将 XML 和 XSL 放在单独的字符串中,它们不在磁盘上,出于安全原因,我不能将它们写入磁盘。关于如何让这些从字符串而不是磁盘文件工作的任何建议?

蒂亚!

4

1 回答 1

3

这是一个 C# 示例——将其转换为 VB 留给读者作为练习 :) )

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());
        }
    }
}
于 2012-07-28T16:52:03.047 回答