C# How to perform a live xslt transformation on an in memory object?
        Posted  
        
            by JL
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by JL
        
        
        
        Published on 2010-03-30T20:50:02Z
        Indexed on 
            2010/03/30
            22:03 UTC
        
        
        Read the original article
        Hit count: 379
        
I have a function that takes 2 parameters : 1 = XML file, 2 = XSLT file, then performs a transformation and returns the resulting HTML.
Here is the function:
/// <summary>
/// Will apply an XSLT style to any XML file and return the rendered HTML.
/// </summary>
/// <param name="xmlFileName">
/// The file name of the XML document.
/// </param>
/// <param name="xslFileName">
/// The file name of the XSL document.
/// </param>
/// <returns>
/// The rendered HTML.
/// </returns>
public string TransformXml(string xmlFileName, string xslFileName)
{
    var xtr = new XmlTextReader(xmlFileName)
                  {
                      WhitespaceHandling = WhitespaceHandling.None
                  };
    var xd = new XmlDocument();
    xd.Load(xtr);
    var xslt = new System.Xml.Xsl.XslCompiledTransform();
    xslt.Load(xslFileName);
    var stm = new MemoryStream();
    xslt.Transform(xd, null, stm);
    stm.Position = 1;
    var sr = new StreamReader(stm);
    xtr.Close();
    return sr.ReadToEnd();
}
I want to change the function not to accept a file for the XML, but instead just an object. The object is exactly compatible with the xslt, if it was serialized to file. But I don't want to have to serialize it to a file first.
So to recap : keep the xslt coming from a file, but the xml input should an object I pass and would like to generate the xml from without any file system interaction.
© Stack Overflow or respective owner