Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am creating a sample document using the code below.

C#
public static void CreateDocumentFile(string file)
       {
           // Create a Wordprocessing document.
           using (WordprocessingDocument package = WordprocessingDocument.Create(file, WordprocessingDocumentType.Document))
           {
               // Add a new main document part.
               package.AddMainDocumentPart();

               // Create the Document DOM.
               package.MainDocumentPart.Document =
                 new Document(
                   new Body(
                     new Paragraph(
                       new Run(
                         new Text("Hello World!")))));

               // Save changes to the main document part.
               package.MainDocumentPart.Document.Save();
           }


Document is created at the desired location but when i open it, gives me an error ,

The file cannot be opened because there are some problem in the content and it is pointing that my document.xml file contains error on line:1 col:198

my document.xml contains
<pre lang="xml"><?xml version="1.0" encoding="utf-8"?><w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:body><a:p xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><a:r><a:t>Hello World!</a:t></a:r></a:p></w:body></w:document>
Posted
Comments
Sergey Alexandrovich Kryukov 18-Feb-13 14:39pm    
Can you open this document with Word. Are you using compatible version of Microsoft.Office.Interop?
—SA

Hello,

That's a wrong using statement...

The Paragraph class exists in 3 different namespaces, and I think you chose the wrong one.

When dealing with a Word document you should use DocumentFormat.OpenXml.Wordprocessing.Paragraph

I think you used DocumentFormat.OpenXml.Drawing.Paragraph as the schema shown in your example is the schema for the drawing namespace.

So the solution is simple in your using section, remove:

C#
using DocumentFormat.OpenXml.Drawing;

and replace by the correct one:

C#
using DocumentFormat.OpenXml.Wordprocessing;


Valery.
 
Share this answer
 
Comments
xfarzad 1-Mar-21 12:31pm    
good point Valery, thank you
By testing your code with Open XML Format SDK 2.0[^] (v 2.0.5022.0) [NET 3.5]
or Open XML SDK 2.5[^] (v 2.5.5631.0) [NET 4.0] my output looked like this (output formatted):
XML
<?xml version="1.0" encoding="utf-8"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
 <w:body>
  <w:p>
   <w:r>
    <w:t>Hello World!</w:t>
   </w:r>
  </w:p>
 </w:body>
</w:document>

and I was able to open the Document in Word 2007 and WordPad 6.
 
Share this answer
 
Comments
Valery Possoz 18-Feb-13 17:14pm    
I got the same as you, there's nothing wrong with the code itself, the problem is a bad using. See my answer.

Valery.
Matej Hlatky 18-Feb-13 17:27pm    
Yes, the wordprocessingml vs. drawingml.
Good point, Valery!
m.kashif.ashraf 20-Feb-13 1:53am    
Thanks Valery and Matej for your timely contribution, Valery's solution worked.

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



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