Example XSL Transformation

If you have an XSL document that you wish to apply to you webpart returns, you need a method to apply the XSL transformation.

[csharp]

public string XslTransform(XmlDocument XmlDoc, string xslPath)
{
        System.Xml.Xsl.XslTransform exampleTransform = null;
        StringBuilder exampleBuilder = new StringBuilder(0x2710);
        TextWriter exampleWriter = new StringWriter(exampleBuilder);
        try
        {
                if (this.Page.Cache.Get(xslPath) is System.Xml.Xsl.XslTransform)
                {
                        exampleTransform = this.Page.Cache.Get(“Xsl”) as System.Xml.Xsl.XslTransform;
                }
        }
        catch
        {
        }
        if (exampleTransform == null)
        {
                XmlDocument exampleDocument = new XmlDocument();
                XmlUrlResolver exampleResolver = new XmlUrlResolver();
                exampleResolver.Credentials = CredentialCache.DefaultCredentials;
                exampleDocument.XmlResolver = exampleResolver;
                exampleDocument.Load(xslPath);
                exampleTransform = new System.Xml.Xsl.XslTransform();
                exampleTransform.Load(exampleDocument.CreateNavigator(), exampleResolver, null);
                try
                {
                        this.Page.Cache.Insert(xslPath, exampleTransform);
                }
                catch
                {
                }
        }
        exampleTransform.Transform(XmlDoc.CreateNavigator(), null, exampleWriter, null);
        exampleWriter.Close();
        return exampleBuilder.ToString();
}

[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *