September 2010 - Posts

I am going to write this entry once I get some time. Till if anybody have already any idea and wish to have code for this, I have created a small sample application with this entry. Please find it in attachment. Click here if you don't find the attachment (you may not able to see post attachment in aggregate view)

hope it helps./.

My friend developers, I have something this time handy code block which is useful to transform any Xml data with the desired Xslt output file. Below code snippet provides you line by line code comment for better understandability. The function requires two arguments; those are: the physical xslt file path and the xml string to transform. The function returns transformed raw Html based on the Xslt document you have written.

The Important elements in this functionality are:

    * XMLDocument object- to load the xml string and xslt file
    * XslTransform object- to actual perform the Transformation
    * MemoryStream & StreamWriter object- to which transformed output will be written.
    * StreamReader object- to read the resultant output written in memoty stream.
    * XmlUrlResolver object- to resolve external XML resources such as entities, document type definitions (DTDs) or schemas.
 

        public string TransformedOutput(string strXSLTFileToTransform, string strXMLToTransform)
        {
            //--final result will be written to strResult
            string strResult = string.Empty;

            try
            {
                //--declare memory stream and writer associated with to which we want to write the output of the transformed file
                MemoryStream memStream = new MemoryStream();
                System.IO.StreamWriter writer = new System.IO.StreamWriter(memStream, System.Text.Encoding.UTF8);
 
                //--xml doc var to load xslt file and xml string
                //--xml doc to load xml string data
                XmlDocument xDoc = new XmlDocument();

                //--xml doc to load xslt file
                XmlDocument xsDoc = new XmlDocument();

                //--xsl transform object to actually load the xslt file and perform transform
                XslTransform xslDoc = new XslTransform();

                //--resolver require if incase; there are any external xml resources like entity or schema
                XmlUrlResolver resolver = new XmlUrlResolver();

                try
                {
                    // Create an XmlUrlResolver with default credentials.
                    resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;

                    //--load xsl file
                    xDoc.LoadXml(strXMLToTransform);

                    //--xml string
                    xsDoc.Load(strXSLTFileToTransform);

                    //--load the xst XmlDocument object to transform
                    xslDoc.Load(xsDoc, resolver, this.GetType().Assembly.Evidence);

                    //--actual transform and write the output to stream writer
                    xslDoc.Transform(xDoc, null, writer, null);

                    //--flush
                    writer.Flush();
                    memStream.Position = 0;
                    StreamReader oReader = new StreamReader(memStream);

                    //--get the output to render in a string var
                    strResult = oReader.ReadToEnd();
                }
                catch (Exception Ex) { throw Ex; }
                finally
                {
                    if (writer != null)
                        writer.Close();
                }
                return strResult;
            }
            catch (Exception Ex)
            { throw Ex; }
        }
 

This is for my future reference as well as for anybody who find it useful.
Hope it helps./.