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);

Retrieve Sharepoint List Attachment programatically...

There is two was you can achieve this:

1.
foreach (string fileName in item.Attachments)
  {
    SPFile file = item.ParentList.ParentWeb.GetFile(
      item.Attachments.UrlPrefix + fileName);
  }



2. 

       SPListItem item = itemColl[0];
       SPFolder folder = ReferenceWeb.Folders["Lists"].SubFolders["Shared Assumption                  Translations"].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
                            foreach (SPFile file in folder.Files)
                            {
                                byte[] binFile = file.OpenBinary();
                                memStr.Write(binFile, 0, binFile.Length);
                            }