将 null 传递给“XslCompiledTransform.Transform”方法
我正在尝试使用 XSL 转换 XML 文档。我不太熟悉如何在 .NET 中转换 XML,所以我使用一些示例代码...
XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));
MemoryStream ms = new MemoryStream();
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string output = sr.ReadToEnd();
ms.Close();
Response.Write(output);
我遇到的问题是以下行...
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
编译器在传入 null< 时显示错误/code> 到 Transform 方法。在查看
Transform
方法签名时,参数 null
看起来代表的是转换的各种选项。 Visual Studio IDE 甚至显示该值可以为 null
。我还尝试使用 DBNull.Value,但这也显示错误。知道我缺少什么吗?
更新:我收到的错误有两个。 (1) 首先,我被告知不再支持 null
,我应该使用 System.DBNull
。 (2) 将 null
更改为 System.DBNull
后,我收到一条错误消息,指出重载解析失败,因为无法使用这些参数调用可访问的 Transform。
I am trying to transform and XML document using XSL. I am not too familiar with how to transform XML in .NET so I am using some example code ...
XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));
MemoryStream ms = new MemoryStream();
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string output = sr.ReadToEnd();
ms.Close();
Response.Write(output);
The problem I am running into is the following line ...
xslTransformer.Transform(Server.MapPath("Test.xml"), null, ms);
The compiler is showing an error in passing in null
to the Transform method. In looking at the Transform
method signatures it looks like the parameter null
is representing is various options for the transformation. The Visual Studio IDE even shows that this value can be null
. I also tried using DBNull.Value
but that also shows an error. Any idea what I am missing?
UPDATE: The error I am receiving is two-fold. (1) First I am told that null
is no longer supported and I should use System.DBNull
. (2) Once I change null
to System.DBNull
I get an error saying overload resolution failed because no accessible Transform can be called with these arguments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试使用不同的 XslCompiledTransform.Transform 方法重载,接受字符串路径和 XmlWriter :
实际上,如果您只对将 XML 呈现为字符串感兴趣,则可以使用
StringBuilder
作为目标,没有MemoryStream
,并将代码简化为:[Edit](根据您的评论)
如果输入 XML 位于字符串中,那么您可以使用另一个
XslCompiledTransform.Transform
方法重载:转换(XmlReader,XmlWriter)
。XmlReader
可以(以及其他方式)使用TextReader
的具体实现进行实例化,在本例中是StringReader
。您还可以使用其他流作为输入。当您将所有这些放在一起时,您最终应该得到:
所有这些类都实现了 IDisposable,因此您需要确保它们在使用后被处置。
You can try using a different XslCompiledTransform.Transform method overload, the one which accepts a String path and an XmlWriter:
Actually, if you are only interesting in rendering XML to a string, you can use a
StringBuilder
as the target, without aMemoryStream
, and simplify your code to:[Edit] (based on your comment)
If the input XML is in a string, then you can use yet another
XslCompiledTransform.Transform
method overload:Transform(XmlReader, XmlWriter)
.XmlReader
can be (among other ways) instantiated using a concrete implementation ofTextReader
, in this case aStringReader
. You can also use an other stream as input.When you put all that together, you should end up with:
All of these classes implement
IDisposable
, so you need to make sure they are disposed after use.您确定是中间的参数导致了错误吗?也许
Server.MapPath("Test.xml")
返回 null?Are you sure it's the middle argument that's causing the error? Perhaps
Server.MapPath("Test.xml")
is returning null?