Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET XML: What is the .NET equivalent of XmlDocument.TransformNode?
    text
    copied!<p>In native programming the IXMLDOMDocument2 object had a <a href="http://msdn.microsoft.com/en-us/library/ms896487.aspx" rel="nofollow noreferrer"><code>tranformNode()</code></a> method:</p> <pre><code>public BSTR transformNode(IXMLDOMNode stylesheet); </code></pre> <p>So in the end I could transform an XML document using:</p> <pre><code>public string TransformDocument(IXMLDOMDocument2 doc, IXMLDOMDocument2 stylesheet) { return doc.TransformNode(stylesheet); } </code></pre> <p>I'm trying to find the managed equivalent. I've already discovered <a href="http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx" rel="nofollow noreferrer">XmlDocument</a> object:</p> <pre><code>public string TransformDocument(XmlDocument doc, XmlDocument stylesheet) { //return doc.TransformNode(stylesheet); //TransformNode not supported } </code></pre> <p>So what is the managed way to transform xml?</p> <p>I've stumbled across the <strong>deprecated</strong> <a href="http://msdn.microsoft.com/en-us/library/system.xml.xsl.xsltransform.aspx" rel="nofollow noreferrer">XslTransform</a> object, but none of the 18 overloads takes an xml document, or an xml stylesheet.</p> <p>The replacement Microsoft indicates is the mouthful: <a href="http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx" rel="nofollow noreferrer">System.Xml.Xsl.XslCompiledTransform</a>. But like it's deprecated cousin, none of XslCompiledTransform's 14 overloads takes xml in an input parameter.</p> <p>So what's the accepted method to transform xml in C# .NET 2.0?</p> <p>Put it another way: complete the following helper method:</p> <pre><code>public string TransformDocument(XmlDocument doc, XmlDocument stylesheet) { //todo: figure out how to transform xml in C# } </code></pre> <hr> <h2>Answer</h2> <p><a href="https://stackoverflow.com/questions/470286/-net-xml-what-is-the-net-equivalent-of-xmldocument-transformnode#470405">Waqas</a> had the answer. Here is another, very similar, solution:</p> <pre><code>/// &lt;summary&gt; /// This method simulates the XMLDOMDocument.TransformNode method /// &lt;/summary&gt; /// &lt;param name="doc"&gt;XML document to be transformed&lt;/param&gt; /// &lt;param name="stylesheet"&gt;The stylesheet to transform with&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; 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(); } </code></pre> <p><strong>Note:</strong> 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.</p> <pre><code>public static string Transform(XmlDocument doc, XslCompiledTransform stylesheet) { ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload