Thursday, October 6, 2011

Programatically applying XSL to XML file

1. Read the XSL file and load into Memory Stream. 

           MemoryStream mstrTransXSL = getXSL(qryTransType);
           mstrTransXSL.Position = 0;
          XPathDocument inputXSLDoc = new XPathDocument(mstrTransXSL);

       //Load the xsl File...
        XslCompiledTransform myXslTransform = new XslCompiledTransform();
        myXslTransform.Load(inputXSLDoc);

2. Create the String Writer object to load the transformed data.

            StringWriter sw = new StringWriter();
           XmlTextWriter writer = new XmlTextWriter(sw);//writer for output file..

3. Load the XML file .                    
      StringReader rdCommonXMReader = new StringReader(strCommonXML);
     XPathDocument CommonXMReaderXPathDocument = new   XPathDocument(rdCommonXMReader);

       //Get Arguments for transformation..

     XsltArgumentList xslArg = new XsltArgumentList();
    xslArg.AddParam("Day", "", day);//Here day and hour are calculated first and assign to the  variable.
    xslArg.AddParam("Hour", "", hour);

  4. Perform the transformation.
               
       myXslTransform.Transform(CommonXMReaderXPathDocument, xslArg, writer);
       writer.Close();
5.Finally load the transformed XML into string object.

    string strTrasnformedXML = Convert.ToString(sw);

No comments: