.NET XML:XmlDocument.TransformNode的.NET等效项是什么? [英] .NET XML: What is the .NET equivalent of XmlDocument.TransformNode?
问题描述
在本机编程中,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?
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等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!