System.IO.IOException after upgrade to OpenXML SDK 2.6

Home Forums Open-Xml-Sdk System.IO.IOException after upgrade to OpenXML SDK 2.6

This topic contains 2 replies, has 2 voices, and was last updated by  sdixon 8 years, 6 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #3431

    sdixon
    Participant

    Hello – I am running into an exception after upgrading to 2.6…the “part.GetStream” is failing:

    private static void FlushPart(OpenXmlPart part, HashSet<OpenXmlPart> visited)
            {
                visited.Add(part);
                XDocument xdoc = part.Annotation<XDocument>();
                if (xdoc != null && xdoc.Annotation<ChangedSemaphore>() != null)
                {
                    using (XmlWriter xw = XmlWriter.Create(part.GetStream(FileMode.Create, FileAccess.Write)))
                    {...

    The exception is:

    {System.IO.IOException: Entries cannot be opened multiple times in Update mode.
    at System.IO.Compression.ZipArchiveEntry.OpenInUpdateMode()
    at System.IO.Packaging.ZipStreamManager.Open(ZipArchiveEntry zipArchiveEntry, FileMode streamFileMode, FileAccess streamFileAccess)
    at System.IO.Packaging.ZipPackagePart.GetStreamCore(FileMode streamFileMode, FileAccess streamFileAccess)
    at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
    at OpenXml.PowerTools.DocumentExtensions.FlushPart…

    Any guidance as to how to prevent or work around this issue (as the same code worked fine before 2.6)?

    Thanks!

    • This topic was modified 8 years, 6 months ago by  sdixon.
    • This topic was modified 8 years, 6 months ago by  sdixon.
    #3434

    Eric White
    Keymaster

    The new System.IO.Packaging is based on the System.IO.Compression namespace, and a constraint of System.IO.Compression is that you can’t open a file in the zip package more than once for writing. This makes sense – what would the behavior be if a file is opened more than once for writing? It would be pretty random.

    If you are seeing this in code that is based on the Open-Xml-PowerTools, then you will need to upgrade to the latest. I have fixed all such issues in Open-Xml-PowerTools.

    If you are seeing this in code that you wrote, then you will need to find out why you are opening a part for writing more than once, and then change your code so that you don’t do this. In most cases, you might be opening a part for writing where you actually didn’t need to do so – in one location in your code, you might be opening the document for writing, where you don’t actually change the markup, whereas in another area of your code, you will be writing to the part. In this case, change your code so that you are opening for writing only when you actually need to do so.

    I hope this made sense to you. This is the gist, and it is a hard limitation with System.IO.Compression, so the issue must be fixed.

    #3435

    sdixon
    Participant

    Yes..makes sense…thank you very much (especially for the quick reply).

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.