XSLT transformation in C# with XslCompiledTransform

I think, this is the simplest solution of the "in memory XML transformation" with XslCompiledTransform! I can't find anywhere the right solution, so here it is, working for me! 🙂
Language: C#

private string DoTransform(string xml, string xsl)
{
System.Io.MemoryStream memstr = new System. Io.MemoryStream(Encoding.Default.GetBytes(xml));
System.Io.StringWriter sw = new System.Io.StringWriter();
System.Xml.XPath.XPathDocument myXPathDoc = new System.Xml.XPath.XPathDocument(memstr);
System.Xml.Xsl.XslCompiledTransform Trans = new System.Xml.Xsl.XslCompiledTransform(true);
System.Xml.XmlDocument xsldoc = new XmlDocument();
xsldoc.LoadXml(xsl);
Trans.Load(xsldoc);
Trans.Transform(myXPathDoc, null, sw);
return sw.ToString();
}

You can download my Transformation class from this location: XSLTransformation.cs
Hope, this help You!

Related Posts