Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
Can any one tell me why GetStream(FileMode.CreateNew, is erroring with Filemode not supported please. Code as follows

C#
static void Main(string[] args)
        {
            using (Package Docpackage = Package.Open("HelloWorld.docx"))
            {
                // Create the mail part
                PackagePart mainPart = Docpackage.CreatePart(new Uri("/document.xml", UriKind.Relative), "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

                // and the relationship
                Docpackage.CreateRelationship(mainPart.Uri, TargetMode.Internal,"http://.../officeDocument/2006/relationships/officeDocument");
                 // create the empty document XML
                using (XmlWriter writer = XmlWriter.Create(mainPart.GetStream(FileMode.CreateNew, FileAccess.ReadWrite)))
                {
                    writer.WriteStartElement("w", "document", "http//.../wordprocessingml/2006/main");
                    writer.WriteEndElement();
                }
                  
            }
        }


What I have tried:

only the code above as I have never used package before
Posted
Updated 1-Apr-20 5:54am
v2
Comments
CHill60 1-Apr-20 11:55am    
Looks like you don't have write permission to that location
Member 13035567 2-Apr-20 7:54am    
HI thank you for your help. The sample code is from the book OPenXML explained by Wouter Van Vugt so I am just trying to follow the book. The file Helloworlt.docx is created ok but fails on the creation of the XML details. I have checked the folder and I do have full access to the folder, so at a loss at the moment
PIEBALDconsult 1-Apr-20 12:57pm    
Maybe because you're applying it to the stream you want to read from?
You probably only need to move a right-parenthesis.
Daniele Rota Nodari 20-Apr-20 5:40am    
What are the exact error exception and message?
If it refers only to FileMode, than could be that the GetStream method does not support that specific mode; usually FileMode.CreateNow is used to create (and write) a new file, not to read an existing one.
Write responsibility in your code seems delegated to XmlWriter only and mainPart.GetStream seems to return (read) a stream of existing data.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900