以下代码工作并从本地磁盘获取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分别放在单独的字符串中,它们不在磁盘上,出于安全原因我不能将它们写入磁盘。关于如何从字符串而不是从磁盘文件中获取这些工作的任何建议?
TIA!
答案 0 :(得分: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());
}
}
}