Monday, June 16, 2008

How to open a document file in Sharepoint document library and load into an XML?

SPSite site = new SPSite(siteurl); //To get the site collection -http://localhost:801/sites/NDA - Sharepoint Site URL
SPWeb web = site.OpenWeb(); // To get the site
SPList list = web.Lists[DocLibraryName];//To get the list with the given list name -UBA - Document Library Name
//Loop through each item in the document library
foreach (SPListItem listitem in list.Items)
{
SPFile file = listitem.File; //get the list item
byte[] binaryFile = file.OpenBinary(); // open the list item and add it to binary file
MemoryStream inStream = new MemoryStream(binaryFile); //Load the binary file into memory stream
XmlTextReader reader = new XmlTextReader(inStream);
XmlDocument xd = new XmlDocument();
xd.Load(reader); // Load the document into an xml file
reader.Close();
inStream.Close();
}

No comments: