将 null 传递给“XslCompiledTransform.Transform”方法

发布于 2024-11-05 20:24:58 字数 991 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝不放开 2024-11-12 20:24:58

您可以尝试使用不同的 XslCompiledTransform.Transform 方法重载,接受字符串路径和 XmlWriter

XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));

// XmlWriter allows you to specify additional settings
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

using (MemoryStream ms = new MemoryStream())
{
    using (XmlWriter writer = XmlWriter.Create(ms, settings))
    {
        xslTransformer.Transform(Server.MapPath("Test.xml"), writer);
    }

    ms.Seek(0, SeekOrigin.Begin);
    StreamReader sr = new StreamReader(ms);
    string output = sr.ReadToEnd();
    ms.Close();
}

Response.Write(output); 

实际上,如果您只对将 XML 呈现为字符串感兴趣,则可以使用 StringBuilder 作为目标,没有 MemoryStream,并将代码简化为:

XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));

StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
    xslTransformer.Transform(Server.MapPath("Test.xml"), writer);
}

Response.Write(sb.ToString()); 

[Edit](根据您的评论)

如果输入 XML 位于字符串中,那么您可以使用另一个 XslCompiledTransform.Transform 方法重载:转换(XmlReader,XmlWriter)

XmlReader 可以(以及其他方式)使用 TextReader 的具体实现进行实例化,在本例中是 StringReader。您还可以使用其他流作为输入。

当您将所有这些放在一起时,您最终应该得到:

string input = @"<SomeXml />"
StringBuilder output = new StringBuilder();
using (StringReader strReader = new StringReader(input))
{
   using (XmlReader reader = XmlReader.Create(strReader))
   {
      using (XmlWriter writer = XmlWriter.Create(output))
      {
          xslTransformer.Transform(reader, writer);
      }
   }
}
Response.Write(output.ToString()); 

所有这些类都实现了 IDisposable,因此您需要确保它们在使用后被处置。

You can try using a different XslCompiledTransform.Transform method overload, the one which accepts a String path and an XmlWriter:

XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));

// XmlWriter allows you to specify additional settings
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.ConformanceLevel = ConformanceLevel.Fragment;
settings.CloseOutput = false;

using (MemoryStream ms = new MemoryStream())
{
    using (XmlWriter writer = XmlWriter.Create(ms, settings))
    {
        xslTransformer.Transform(Server.MapPath("Test.xml"), writer);
    }

    ms.Seek(0, SeekOrigin.Begin);
    StreamReader sr = new StreamReader(ms);
    string output = sr.ReadToEnd();
    ms.Close();
}

Response.Write(output); 

Actually, if you are only interesting in rendering XML to a string, you can use a StringBuilder as the target, without a MemoryStream, and simplify your code to:

XslCompiledTransform xslTransformer = new XslCompiledTransform();
xslTransformer.Load(Server.MapPath("Test.xslt"));

StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
    xslTransformer.Transform(Server.MapPath("Test.xml"), writer);
}

Response.Write(sb.ToString()); 

[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 of TextReader, in this case a StringReader. You can also use an other stream as input.

When you put all that together, you should end up with:

string input = @"<SomeXml />"
StringBuilder output = new StringBuilder();
using (StringReader strReader = new StringReader(input))
{
   using (XmlReader reader = XmlReader.Create(strReader))
   {
      using (XmlWriter writer = XmlWriter.Create(output))
      {
          xslTransformer.Transform(reader, writer);
      }
   }
}
Response.Write(output.ToString()); 

All of these classes implement IDisposable, so you need to make sure they are disposed after use.

雨落□心尘 2024-11-12 20:24:58

您确定是中间的参数导致了错误吗?也许 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文