XslCompiledTransform 输出为 XPathDocument

发布于 2024-08-04 01:50:24 字数 73 浏览 9 评论 0原文

我正在尝试使用 XslCompiledTransform,并将输出用作 XPathDocument。

有什么想法吗?

I am trying to use a XslCompiledTransform, and use the output as a XPathDocument.

Any Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

坏尐絯℡ 2024-08-11 01:50:24

琼斯先生的回答对我很有帮助,但我发现最后一行不起作用。我最终这样做了:(

XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(filePath);
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlTextWriter.Create(stringWriter);
xsl.Transform(xPathDoc, xmlWriter);
String newXml = stringWriter.ToString();
StringReader stringReader = new StringReader(newXml);
xPathDoc = new XPathDocument(stringReader);

这里,xPathDoc 是一个已经从 XmlReader 初始化的 XPathDocument。)

Mr. Jones's answer was very helpful for me, but I found that the last line didn't work. I ended up doing this:

XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(filePath);
StringWriter stringWriter = new StringWriter();
XmlWriter xmlWriter = XmlTextWriter.Create(stringWriter);
xsl.Transform(xPathDoc, xmlWriter);
String newXml = stringWriter.ToString();
StringReader stringReader = new StringReader(newXml);
xPathDoc = new XPathDocument(stringReader);

(Here, xPathDoc is an XPathDocument that has already been initialized from an XmlReader.)

孤檠 2024-08-11 01:50:24

将转换发送到基于 StringWriterXmlTextWriter。然后通过从 StringWriter 检索 XML 字符串来实例化 XPathDocument

var sw = new StringWriter();
var xtw = new XmlTextWriter(sw);

myTransform.Transform(myXml, xtw);

var xpd = new XPathDocument(sw.ToString());

它不是最高效的内存机制,但足以满足大多数需求。类似的方法是使用 MemoryStream 而不是 StringWriter,但相比之下它有点混乱。

Send the transform to an XmlTextWriter based on a StringWriter. Then instance the XPathDocument by retreiving the XML string from the StringWriter.

var sw = new StringWriter();
var xtw = new XmlTextWriter(sw);

myTransform.Transform(myXml, xtw);

var xpd = new XPathDocument(sw.ToString());

Its not the most memory efficient mechanism but will be adequate for most needs. A similar approach would be use a MemoryStream instead of a StringWriter but its a little messy by comparison.

奢华的一滴泪 2024-08-11 01:50:24

David M. Anderson 的答案稍微好一点的形式如下:它不会遭受潜在的资源泄漏;否则都是一样的。

private static XPathDocument TransformToXPathDocument(string styleSheetPath,
                                                      IXPathNavigable xPathDoc)
{
    var xsl = new XslCompiledTransform();
    xsl.Load(styleSheetPath);
    using(var stringWriter = new StringWriter())
    {
        using(XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
        {
            xsl.Transform(xPathDoc, xmlWriter);
        }
        using(var reader = new StringReader(stringWriter.ToString()))
        {
            return new XPathDocument(reader);
        }
    }
}

A slightly better form of David M. Anderson's answer is below: it does not suffer from potential resource leaks; otherwise it is the same.

private static XPathDocument TransformToXPathDocument(string styleSheetPath,
                                                      IXPathNavigable xPathDoc)
{
    var xsl = new XslCompiledTransform();
    xsl.Load(styleSheetPath);
    using(var stringWriter = new StringWriter())
    {
        using(XmlWriter xmlWriter = XmlWriter.Create(stringWriter))
        {
            xsl.Transform(xPathDoc, xmlWriter);
        }
        using(var reader = new StringReader(stringWriter.ToString()))
        {
            return new XPathDocument(reader);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文