예제 #1
0
        /// <summary>
        /// Applies a XSLT to a <see cref="XmlDocument" /> and returns another <see cref="XmlDocument" />.
        /// </summary>
        /// <param name="doc">The source <see cref="XmlDocument" /></param>
        /// <param name="xslt">The XSLT to apply.</param>
        /// <returns>
        /// The transformed <see cref="XmlDocument" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static IXPathNavigable Transform(IXPathNavigable doc, XslCompiledTransform xslt)
        {
            doc.ThrowIfNull(nameof(doc));

            xslt.ThrowIfNull(nameof(xslt));

            MemoryStream stream = GetMemoryStream();

            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
            {
                // Apply the XSL transform
                writer.Formatting = Formatting.Indented;
                xslt.Transform(doc, writer);
                writer.Flush();

                // Reads the XML document from the given stream.
                stream.Seek(0, SeekOrigin.Begin);
                XmlDocument outDoc = new XmlDocument();
                outDoc.Load(stream);
                return(outDoc);
            }
        }