<p>I want to apply an XSLT Stylesheet to an XML Document using C# and write the output to a File.</p>

[{'answer_id': 34095, 'author': 'Daren Thomas', 'author_id': 2260, 'author_profile': 'https://Stackoverflow.com/users/2260', 'pm_score': 9, 'selected': True, 'text': '<p>I found a possible answer here: <a href="http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63" rel="noreferrer">http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63</a></p>\n\n<p>From the article:</p>\n\n<pre><code>XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;\nXslTransform myXslTrans = new XslTransform() ;\nmyXslTrans.Load(myStyleSheet);\nXmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;\nmyXslTrans.Transform(myXPathDoc,null,myWriter) ;\n</code></pre>\n\n<p><strong>Edit:</strong></p>\n\n<p>But my trusty compiler says, <code>XslTransform</code> is obsolete: Use <code>XslCompiledTransform</code> instead:</p>\n\n<pre><code>XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;\nXslCompiledTransform myXslTrans = new XslCompiledTransform();\nmyXslTrans.Load(myStyleSheet);\nXmlTextWriter myWriter = new XmlTextWriter("result.html",null);\nmyXslTrans.Transform(myXPathDoc,null,myWriter);\n</code></pre>\n'}, {'answer_id': 4022970, 'author': 'ManBugra', 'author_id': 162192, 'author_profile': 'https://Stackoverflow.com/users/162192', 'pm_score': 4, 'selected': False, 'text': '<p>Here is a tutorial about how to do XSL Transformations in C# on MSDN:</p>\n\n<p><a href="http://support.microsoft.com/kb/307322/en-us/" rel="noreferrer">http://support.microsoft.com/kb/307322/en-us/</a></p>\n\n<p>and here how to write files:</p>\n\n<p><a href="http://support.microsoft.com/kb/816149/en-us" rel="noreferrer">http://support.microsoft.com/kb/816149/en-us</a></p>\n\n<p>just as a side note: if you want to do validation too here is another tutorial (for DTD, XDR, and XSD (=Schema)):</p>\n\n<p><a href="http://support.microsoft.com/kb/307379/en-us/" rel="noreferrer">http://support.microsoft.com/kb/307379/en-us/</a> </p>\n\n<p>i added this just to provide some more information.</p>\n'}, {'answer_id': 10596241, 'author': 'Heinzi', 'author_id': 87698, 'author_profile': 'https://Stackoverflow.com/users/87698', 'pm_score': 7, 'selected': False, 'text': '<p>Based on Daren\'s excellent answer, note that this code can be shortened significantly by using the appropriate <a href="http://msdn.microsoft.com/en-us/library/ms163431.aspx">XslCompiledTransform.Transform overload</a>:</p>\n\n<pre><code>var myXslTrans = new XslCompiledTransform(); \nmyXslTrans.Load("stylesheet.xsl"); \nmyXslTrans.Transform("source.xml", "result.html"); \n</code></pre>\n\n<p><em>(Sorry for posing this as an answer, but the <code>code block</code> support in comments is rather limited.)</em></p>\n\n<p>In VB.NET, you don\'t even need a variable:</p>\n\n<pre><code>With New XslCompiledTransform()\n    .Load("stylesheet.xsl")\n    .Transform("source.xml", "result.html")\nEnd With\n</code></pre>\n'}, {'answer_id': 58930747, 'author': 'Vinod Srivastav', 'author_id': 3057246, 'author_profile': 'https://Stackoverflow.com/users/3057246', 'pm_score': 2, 'selected': False, 'text': '<p>This might help you</p>\n\n<pre class="lang-cs prettyprint-override"><code>public static string TransformDocument(string doc, string stylesheetPath)\n{\n    Func&lt;string,XmlDocument&gt; GetXmlDocument = (xmlContent) =&gt;\n     {\n         XmlDocument xmlDocument = new XmlDocument();\n         xmlDocument.LoadXml(xmlContent);\n         return xmlDocument;\n     };\n\n    try\n    {\n        var document = GetXmlDocument(doc);\n        var style = GetXmlDocument(File.ReadAllText(stylesheetPath));\n\n        System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();\n        transform.Load(style); // compiled stylesheet\n        System.IO.StringWriter writer = new System.IO.StringWriter();\n        XmlReader xmlReadB = new XmlTextReader(new StringReader(document.DocumentElement.OuterXml));\n        transform.Transform(xmlReadB, null, writer);\n        return writer.ToString();\n    }\n    catch (Exception ex)\n    {\n        throw ex;\n    }\n\n}   \n</code></pre>\n'}, {'answer_id': 72905605, 'author': 'Mubashar', 'author_id': 806076, 'author_profile': 'https://Stackoverflow.com/users/806076', 'pm_score': 0, 'selected': False, 'text': "<p>I would like to share this small piece of code which reads from Database and transforms using XSLT. On the top I also have used <code>xslt-extensions</code> which makes it little different than others.</p>\n<p>Note: <em>This is just a draft code and may need cleanup before using in production.</em></p>\n<pre><code>var schema = XDocument.Load(XsltPath);\nusing (var connection = new SqlConnection(ConnectionString))\n{\n    connection.Open();\n    using (var command = new SqlCommand(Sql, connection))\n    {\n        var reader = command.ExecuteReader();\n        var dt = new DataTable(SourceNode);\n        dt.Load(reader);\n                    \n        string xml = &quot;&lt;?xml version=\\&quot;1.0\\&quot; encoding=\\&quot;UTF-8\\&quot;?&gt;&quot; + Environment.NewLine;\n        using (var stringWriter = new StringWriter())\n        {\n            dt.WriteXml(stringWriter, true);\n            xml += stringWriter.GetStringBuilder().ToString();\n        }\n\n        XDocument transformedXml = new XDocument();\n        var xsltArgumentList = new XsltArgumentList();\n        xsltArgumentList.AddExtensionObject(&quot;urn:xslt-extensions&quot;, new XsltExtensions());\n\n        using (XmlWriter writer = transformedXml.CreateWriter())\n        {\n            XslCompiledTransform xslt = new XslCompiledTransform();\n            xslt.Load(schema.CreateReader());\n            xslt.Transform(XmlReader.Create(new StringReader(xml)), xsltArgumentList, writer);\n        }\n        var result = transformedXml.ToString();\n    }\n}\n</code></pre>\n<p><code>XsltPath</code> is path to your xslt file.<br />\n<code>ConnectionString</code> constant is pointing to your database.<br />\n<code>Sql</code> is your query.<br />\n<code>SourceNode</code> is node of each record in source xml.</p>\n<p>Now the interesting part, please note the use of <code>urn:xslt-extensions</code> and <code>new XsltExtensions()</code> in above code. You can use this if need some complex computation which may not be possible in xslt. Following is a simple method to format date.</p>\n<pre><code>public class XsltExtensions\n{\n    public string FormatDate(string dateString, string format)\n    {\n        DateTime date;\n\n        if (DateTime.TryParse(dateString, out date))\n            return date.ToString(format);\n\n        return dateString;\n    }\n}\n</code></pre>\n<p>In XSLT file you can use it as below;</p>\n<pre><code>&lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot; xmlns:ext=&quot;urn:xslt-extensions&quot;&gt;\n...\n&lt;myTag&gt;&lt;xsl:value-of select=&quot;ext:FormatDate(record_date, 'yyyy-MM-dd')&quot;/&gt;&lt;/myTag&gt;\n...\n&lt;/xsl:stylesheet&gt;\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/34093', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2260/']