How to apply an XSLT Stylesheet in C#

C#XmlXslt

C# Problem Overview


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

C# Solutions


Solution 1 - C#

I found a possible answer here: http://web.archive.org/web/20130329123237/http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63

From the article:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslTransform myXslTrans = new XslTransform() ;
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null) ;
myXslTrans.Transform(myXPathDoc,null,myWriter) ;

Edit:

But my trusty compiler says, XslTransform is obsolete: Use XslCompiledTransform instead:

XPathDocument myXPathDoc = new XPathDocument(myXmlFile) ;
XslCompiledTransform myXslTrans = new XslCompiledTransform();
myXslTrans.Load(myStyleSheet);
XmlTextWriter myWriter = new XmlTextWriter("result.html",null);
myXslTrans.Transform(myXPathDoc,null,myWriter);

Solution 2 - C#

Based on Daren's excellent answer, note that this code can be shortened significantly by using the appropriate XslCompiledTransform.Transform overload:

var myXslTrans = new XslCompiledTransform(); 
myXslTrans.Load("stylesheet.xsl"); 
myXslTrans.Transform("source.xml", "result.html"); 

(Sorry for posing this as an answer, but the code block support in comments is rather limited.)

In VB.NET, you don't even need a variable:

With New XslCompiledTransform()
    .Load("stylesheet.xsl")
    .Transform("source.xml", "result.html")
End With

Solution 3 - C#

Here is a tutorial about how to do XSL Transformations in C# on MSDN:

http://support.microsoft.com/kb/307322/en-us/

and here how to write files:

http://support.microsoft.com/kb/816149/en-us

just as a side note: if you want to do validation too here is another tutorial (for DTD, XDR, and XSD (=Schema)):

http://support.microsoft.com/kb/307379/en-us/

i added this just to provide some more information.

Solution 4 - C#

This might help you

public static string TransformDocument(string doc, string stylesheetPath)
{
	Func<string,XmlDocument> GetXmlDocument = (xmlContent) =>
	 {
		 XmlDocument xmlDocument = new XmlDocument();
		 xmlDocument.LoadXml(xmlContent);
		 return xmlDocument;
	 };

	try
	{
		var document = GetXmlDocument(doc);
		var style = GetXmlDocument(File.ReadAllText(stylesheetPath));

		System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();
		transform.Load(style); // compiled stylesheet
		System.IO.StringWriter writer = new System.IO.StringWriter();
		XmlReader xmlReadB = new XmlTextReader(new StringReader(document.DocumentElement.OuterXml));
		transform.Transform(xmlReadB, null, writer);
		return writer.ToString();
	}
	catch (Exception ex)
	{
		throw ex;
	}

}	

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaren ThomasView Question on Stackoverflow
Solution 1 - C#Daren ThomasView Answer on Stackoverflow
Solution 2 - C#HeinziView Answer on Stackoverflow
Solution 3 - C#ManBugraView Answer on Stackoverflow
Solution 4 - C#Vinod SrivastavView Answer on Stackoverflow