locked
seek(0,0) RRS feed

  • Question

  • I have been using the following code, sometimes it work, sometimes it doesn't. The frmStr.Seek(0,0) sets the write head to the begining of the document, but if what i am writing is a smaller document than the original, the result is that the XML is not well formed.

     

    How can i trim the remaining of the stream before saving it back?

     

    Code Snippet

    // Object to reference the web site

    SPWeb thisWeb = new SPSite(workflowProperties.SiteId).OpenWeb(workflowProperties.WebId);

    // Object to reference item in site.

    SPListItem thisItem = thisWeb.Lists[workflowProperties.ListId].GetItemById(workflowProperties.ItemId);

    // Object to reference the file in the item

    SPFile docFile = thisItem.File;

    // Checkout the document to ensure users can not edit doc while processing.

    docFile.CheckOut();

    // Read the file

    Stream formStr = docFile.OpenBinaryStream();

    XmlDocument formXml = new XmlDocument();

    // Required, if omitted form becomes unusable.

    formXml.PreserveWhitespace = true;

    formXml.Load(formStr);

    // Set the values on the payload form to the values in the task form.

    formXml.GetElementsByTagName("my:StageDescription")[0].InnerText = stageDescription;

    formStr.Seek(0, 0); // this is to move the write head to the begining of the stream, it then overwrites the old information

     

    // Save the document back to SharePoint

    formXml.Save(formStr);

    docFile.SaveBinary(formStr);

    // Check in

    docFile.CheckIn("Workflow Checkin");

     

     

     

    Thanks in advance

    Tuesday, September 11, 2007 4:46 PM

Answers

  • Solved: the above solution was UGLY and well there must of been another way. So what I am doing is setting the length of the xml document to 0, then writing out the modifed content to it.

     

    The SetLength(0) basically wipes out the original content.

     

    Code Snippet

    SPWeb thisWeb = new SPSite(workflowProperties.SiteId).OpenWeb(workflowProperties.WebId);

    // Object to reference item in site.

    SPListItem thisItem = thisWeb.Lists[workflowProperties.ListId].GetItemById(workflowProperties.ItemId);

    // Object to reference the file in the item

    SPFile docFile = thisItem.File;

    // Checkout the document to ensure users can not edit doc while processing.

    docFile.CheckOut();

    // Read the file

    Stream formStr = docFile.OpenBinaryStream();

    XmlDocument formXml = new XmlDocument();

    // Required, if omitted form becomes unusable.

    formXml.PreserveWhitespace = true;

    formXml.Load(formStr);

    // Set the values on the payload form to the values in the task form.

    formXml.GetElementsByTagName("my:StageDescription")[0].InnerText = stageDescription;

     

    formStr.SetLength(0); // Resize to 0

    formStr.Seek(0, 0); // this is to move the write head to the begining of the stream, it then overwrites the old information

     

    // Save the document back to SharePoint

    formXml.Save(formStr);

    docFile.SaveBinary(formStr);

    // Check in

    docFile.CheckIn("Workflow Checkin");

     

     

     

    Wednesday, September 12, 2007 9:27 PM

All replies

  • As a temporary work around, if the new value is shorter than the old value, I am adding spaces to the new value so that it will match the old values length... Ugly I know but it workes.

     

    I would appreciate any advise on alternative methods to use.

     

    Tuesday, September 11, 2007 8:57 PM
  • Solved: the above solution was UGLY and well there must of been another way. So what I am doing is setting the length of the xml document to 0, then writing out the modifed content to it.

     

    The SetLength(0) basically wipes out the original content.

     

    Code Snippet

    SPWeb thisWeb = new SPSite(workflowProperties.SiteId).OpenWeb(workflowProperties.WebId);

    // Object to reference item in site.

    SPListItem thisItem = thisWeb.Lists[workflowProperties.ListId].GetItemById(workflowProperties.ItemId);

    // Object to reference the file in the item

    SPFile docFile = thisItem.File;

    // Checkout the document to ensure users can not edit doc while processing.

    docFile.CheckOut();

    // Read the file

    Stream formStr = docFile.OpenBinaryStream();

    XmlDocument formXml = new XmlDocument();

    // Required, if omitted form becomes unusable.

    formXml.PreserveWhitespace = true;

    formXml.Load(formStr);

    // Set the values on the payload form to the values in the task form.

    formXml.GetElementsByTagName("my:StageDescription")[0].InnerText = stageDescription;

     

    formStr.SetLength(0); // Resize to 0

    formStr.Seek(0, 0); // this is to move the write head to the begining of the stream, it then overwrites the old information

     

    // Save the document back to SharePoint

    formXml.Save(formStr);

    docFile.SaveBinary(formStr);

    // Check in

    docFile.CheckIn("Workflow Checkin");

     

     

     

    Wednesday, September 12, 2007 9:27 PM