Monday, November 01, 2004

XSL-Transformations with C#

Ok, I think I have a functional method for applying xlst's to an xml document. If anyone sees things that don't work, let me know and I'll fix then repost.


public XmlDocument myXmlTransformation(XmlDocument xmlDoc, XmlDocument xsltDoc)

{

    XmlReader xsltReader = new XmlTextReader(xsltDoc.InnerXml, XmlNodeType.Document, null);

    System.Text.StringBuilder tempStringBuilder = new System.Text.StringBuilder();

    XmlWriterSettings xSet = new XmlWriterSettings();

    xSet.Encoding = System.Text.Encoding.UTF32;

    XmlWriter tempXmlWriter = new XmlTextWriter(tempStringBuilder, xSet);

    XsltCommand transCommand = new XsltCommand();



    //Complile and execute against the xml file

    transCommand.Compile(xsltReader);

    transCommand.Execute(sourceDoc, tempXmlWriter);


    //Reset to an XmlDocument object and return

    XmlDocument returnDoc = new XmlDocument();

    returnDoc.LoadXml(tempStringBuilder.ToString());

    return returnDoc;

}


Go figure, I test compile this right before I hit "publish" and something is broken. I'll update when I have a chance.

No comments: