none
Seems to be a bug RRS feed

  • Question

  • Hi ALL,

     

    I'm migrating a code to new April08 SDK version. Sample code in help file seems to be broken, so I came up with this:

     

    Code Snippet

    byte[] content;

    using (MemoryStream ms = new MemoryStream())

    {

    using (WordprocessingDocument package = WordprocessingDocument.Create(ms, WordprocessingDocumentType.Document))

    {

    MainDocumentPart mainPart = package.AddNewPart<MainDocumentPart>(

    "application/vnd.openxmlformats-officedocument.wordprocessingml.main+xml", "rId1");

    using (Stream s = mainPart.GetStream())

    {

    byte[] b = (new UTF8Encoding()).GetBytes(

    @"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>

    <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>");

    s.Write(b, 0, b.Length);

    }

    package.Close();

    }

    ms.Position = 0;

    content = new byte[ms.Length];

    ms.Read(content, 0, (int)ms.Length);

    }

    return content;

     

     

    I got an invalid docx file. Inside [Content_Types].xml file I have this:

     

    Code Snippet

     

    <?xml version="1.0" encoding="utf-8"?>
    <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.main+xml" />
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
    </Types>

     

     

     

    If I change with code below and repack, its works

     

    Code Snippet

     


    <Types xmlns="<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
    <Default Extension="xml" ContentType="application/xml"/>
    <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
    <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml" />
    </Types>


     

     

    My bad? How can I control this behavior? I tried to use

     

    Code Snippet
    package.PartExtensionProvider.MakeSurePartExtensionExist(
    "application/xml", "xml");

     

     

    with no success. Any hint?

     

    TIA,

     

    Rubens

    Tuesday, May 13, 2008 12:36 AM

Answers

  • The sample code has bug.
    Try replace this line

    MainDocumentPart mainPart = package.AddNewPart<MainDocumentPart>(

    "application/vnd.openxmlformats-officedocument.wordprocessingml.main+xml", "rId1");

    to

    MainDocumentPart mainPart = package.AddMainDocumentPart().

     

    Wednesday, May 14, 2008 4:38 AM

All replies

  • The sample code has bug.
    Try replace this line

    MainDocumentPart mainPart = package.AddNewPart<MainDocumentPart>(

    "application/vnd.openxmlformats-officedocument.wordprocessingml.main+xml", "rId1");

    to

    MainDocumentPart mainPart = package.AddMainDocumentPart().

     

    Wednesday, May 14, 2008 4:38 AM
  • Works! Thank you!

     

    Friday, May 16, 2008 12:27 AM