.NET XML:XmlDocument.TransformNode的.NET等效项是什么?

编程入门 行业动态 更新时间:2024-10-26 06:29:01
本文介绍了.NET XML:XmlDocument.TransformNode的.NET等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在本机编程中,IXMLDOMDocument2对象具有 tranformNode() 方法:

In native programming the IXMLDOMDocument2 object had a tranformNode() method:

public BSTR transformNode(IXMLDOMNode stylesheet);

最后,我可以使用以下方法转换XML文档:

So in the end I could transform an XML document using:

public string TransformDocument(IXMLDOMDocument2 doc, IXMLDOMDocument2 stylesheet) { return doc.TransformNode(stylesheet); }

我正在尝试查找托管等效项.我已经发现 XmlDocument 对象:

I'm trying to find the managed equivalent. I've already discovered XmlDocument object:

public string TransformDocument(XmlDocument doc, XmlDocument stylesheet) { //return doc.TransformNode(stylesheet); //TransformNode not supported }

那么转换xml的托管方式是什么?

So what is the managed way to transform xml?

我偶然发现了已弃用的 XslTransform 对象,但18个重载都不使用xml文档或xml样式表.

I've stumbled across the deprecated XslTransform object, but none of the 18 overloads takes an xml document, or an xml stylesheet.

Microsoft所表示的替代品是一口气:系统.Xml.Xsl.XslCompiledTransform .但是像它的表兄一样,XslCompiledTransform的14个重载都没有将xml作为输入参数.

The replacement Microsoft indicates is the mouthful: System.Xml.Xsl.XslCompiledTransform. But like it's deprecated cousin, none of XslCompiledTransform's 14 overloads takes xml in an input parameter.

那么在C#.NET 2.0中转换XML的公认方法是什么?

So what's the accepted method to transform xml in C# .NET 2.0?

用另一种方式:完成以下帮助方法:

Put it another way: complete the following helper method:

public string TransformDocument(XmlDocument doc, XmlDocument stylesheet) { //todo: figure out how to transform xml in C# }

答案

Waqas 有答案.这是另一个非常相似的解决方案:

Answer

Waqas had the answer. Here is another, very similar, solution:

/// <summary> /// This method simulates the XMLDOMDocument.TransformNode method /// </summary> /// <param name="doc">XML document to be transformed</param> /// <param name="stylesheet">The stylesheet to transform with</param> /// <returns></returns> public static string Transform(XmlDocument doc, XmlDocument stylesheet) { XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(stylesheet); // compiled stylesheet System.IO.StringWriter writer = new System.IO.StringWriter(); transform.Transform(doc, XmlWriter.Create(writer)); return writer.ToString(); }

注意::如果您是性能表现不佳的人,则如果要进行多次转换,则可能希望创建一个重载以传递预编译的转换.

Note: If you're a performance weenie, you might want to create an overload to pass the pre-compiled transform, if you're going to transforming more than once.

public static string Transform(XmlDocument doc, XslCompiledTransform stylesheet) { ... }

推荐答案

函数将IXPathNavigable对象作为输入(并且XmlDoucment/XmlNode类实现IXPathNavigable).

The functions take IXPathNavigable objects as input (and XmlDoucment/XmlNode classes implement IXPathNavigable).

这是它的工作方式:

public string TransformDocument(XmlDocument doc, XmlDocument stylesheet) { XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(stylesheet); // compiled stylesheet System.IO.StringWriter writer = new System.IO.StringWriter(); transform.Transform(doc, null, writer); return writer.ToString(); }

优化和改进:

  • 如果多次使用已编译的样式表,则将其缓存.
  • 将XSL直接加载到XslCompiledTransform中,而不是先构建XmlDocument.
  • 使用XmlNode而不是XmlDocument来使该函数更通用.

更多推荐

.NET XML:XmlDocument.TransformNode的.NET等效项是什么?

本文发布于:2023-11-13 23:41:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585630.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:XML   NET   TransformNode   XmlDocument

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!