如何将Open XML字符串转换为Word文档(C#)

10

我正在从代码中读取一个元素,结果得到了Open XML字符串。

  byte[] binary = Convert.FromBase64String(template.Attributes["body"].ToString());
  string bodyContent = UnicodeEncoding.UTF8.GetString(binary);

现在,我想把这个字符串转换为MemoryStream,以便可以通过WordProcessingDocument进行读取,如下所示。
  using (MemoryStream stream = new MemoryStream())
  {
      stream.Write(binary, 0, (int)binary.Length);
        using (WordprocessingDocument wordDoc = wordprocessingDocument.Open(stream, true))
          {
                File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray());
           }      
  }

当我尝试以上代码时,它无法打开流并引发损坏数据的错误。
XML字符串如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" 
        xmlns:v="urn:schemas-microsoft-com:vml" 
        xmlns:w10="urn:schemas-microsoft-com:office:word" 
        xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" 
        xmlns:aml="http://schemas.microsoft.com/aml/2001/core" 
        xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" 
        xmlns:o="urn:schemas-microsoft-com:office:office" 
        xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" 
        xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2" 
        w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
 <w:ignoreElements w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2"/>
 <o:DocumentProperties>
   <o:Title>Follow-up to Our Meeting</o:Title><o:Author>Microsoft Corporation</o:Author><o:LastAuthor>ASI User</o:LastAuthor><o:Revision>2</o:Revision><o:TotalTime>0</o:TotalTime><o:Created>2007-08-10T16:40:00Z</o:Created><o:LastSaved>2007-08-10T16:40:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>178</o:Words><o:Characters>1019</o:Characters><o:Company>Microsoft Corporation</o:Company><o:Lines>8</o:Lines><o:Paragraphs>2</o:Paragraphs><o:CharactersWithSpaces>1195</o:CharactersWithSpaces><o:Version>11.8134</o:Version>
 </o:DocumentProperties>
 <w:fonts>
   <w:defaultFonts w:ascii="Times New Roman" w:fareast="SimSun" w:h-ansi="Times New Roman" w:cs="Times New Roman"/>
  <w:font w:name="SimSun"><w:altName w:val="宋体"/><w:panose-1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="variable"/><w:sig w:usb-0="00000003" w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/></w:font><w:font w:name="@SimSun"><w:panose-1 w:val="02010600030101010101"/><w:charset w:val="86"/><w:family w:val="Auto"/><w:pitch w:val="variable"/><w:sig w:usb-0="00000003" w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000"/></w:font>
 </w:fonts>
 <w:styles>
   <w:versionOfBuiltInStylenames w:val="4"/><w:latentStyles w:defLockedState="off" w:latentStyleCount="156"/><w:style w:type="paragraph" w:default="on" w:styleId="Normal"><w:name w:val="Normal"/><w:rPr><wx:font wx:val="Times New Roman"/><w:sz w:val="24"/><w:sz-cs w:val="24"/><w:lang w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA"/></w:rPr></w:style><w:style w:type="character" w:default="on" w:styleId="DefaultParagraphFont"><w:name w:val="Default Paragraph Font"/><w:semiHidden/></w:style><w:style w:type="table" w:default="on" w:styleId="TableNormal"><w:name w:val="Normal Table"/><wx:uiName wx:val="Table Normal"/><w:semiHidden/><w:rPr><wx:font wx:val="Times New Roman"/></w:rPr><w:tblPr><w:tblInd w:w="0" w:type="dxa"/><w:tblCellMar><w:top w:w="0" w:type="dxa"/><w:left w:w="108" w:type="dxa"/><w:bottom w:w="0" w:type="dxa"/><w:right w:w="108" w:type="dxa"/></w:tblCellMar></w:tblPr></w:style><w:style w:type="list" w:default="on" w:styleId="NoList"><w:name w:val="No List"/><w:semiHidden/></w:style>
 </w:styles>
 <w:docPr>
   <w:view w:val="print"/><w:zoom w:percent="100"/><w:doNotEmbedSystemFonts/><w:attachedTemplate w:val=""/><w:defaultTabStop w:val="720"/><w:characterSpacingControl w:val="DontCompress"/><w:optimizeForBrowser/><w:validateAgainstSchema/><w:saveInvalidXML w:val="off"/><w:ignoreMixedContent w:val="off"/><w:alwaysShowPlaceholderText w:val="off"/><w:compat><w:breakWrappedTables/><w:snapToGridInCell/><w:wrapTextWithPunct/><w:useAsianBreakRules/><w:useWord2002TableStyleRules/><w:useFELayout/></w:compat>
  <wsp:rsids>
    <wsp:rsidRoot wsp:val="00453714"/><wsp:rsid wsp:val="00015B28"/><wsp:rsid wsp:val="000B1B9A"/><wsp:rsid wsp:val="000F4F6A"/><wsp:rsid wsp:val="0010045E"/><wsp:rsid wsp:val="0015095E"/><wsp:rsid wsp:val="0016480E"/><wsp:rsid wsp:val="001B029C"/><wsp:rsid wsp:val="00386D53"/><wsp:rsid wsp:val="003F1099"/><wsp:rsid wsp:val="00453714"/><wsp:rsid wsp:val="004C6C19"/><wsp:rsid wsp:val="004F592D"/><wsp:rsid wsp:val="00511DC8"/><wsp:rsid wsp:val="00583473"/><wsp:rsid wsp:val="005A28AE"/><wsp:rsid wsp:val="005C0D45"/><wsp:rsid wsp:val="005E0E9D"/><wsp:rsid wsp:val="00670858"/><wsp:rsid wsp:val="006C08B9"/><wsp:rsid wsp:val="006C5DCC"/><wsp:rsid wsp:val="007F7DB8"/><wsp:rsid wsp:val="0083137D"/><wsp:rsid wsp:val="009E707D"/><wsp:rsid wsp:val="00AA38F5"/><wsp:rsid wsp:val="00BC08A1"/><wsp:rsid wsp:val="00C0384D"/><wsp:rsid wsp:val="00DE6B6B"/><wsp:rsid wsp:val="00E23D4E"/><wsp:rsid wsp:val="00EA0411"/><wsp:rsid wsp:val="00EB12F6"/>
    </wsp:rsids>
 </w:docPr>
 <w:body>
   <wx:sect>
     <w:p wsp:rsidR="001B029C" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_First_Name" </w:instrText></w:r><w:r wsp:rsidR="00670858"><w:instrText>\f" "</w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_First_Name» </w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Last_Name" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Last_Name»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Business_Unit" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Business_Unit»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p>
    <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"/><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="0083137D"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> DATE  \@ "MMMM d, yyyy"  \* MERGEFORMAT </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="0010045E"><w:rPr><w:noProof/></w:rPr><w:t>August 10, 2007</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"/>
    <w:p wsp:rsidR="005E0E9D" wsp:rsidRDefault="00453714"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> ADDRESSBLOCK \f "&lt;&lt;_FIRST0_&gt;&gt;&lt;&lt; _LAST0_&gt;&gt;&lt;&lt; _SUFFIX0_&gt;&gt;</w:instrText></w:r><w:r><w:cr/><w:instrText>&lt;&lt;_STREET1_</w:instrText></w:r><w:r><w:cr/><w:instrText>&gt;&gt;&lt;&lt;_STREET2_</w:instrText></w:r><w:r><w:cr/><w:instrText>&gt;&gt;&lt;&lt;_CITY_&gt;&gt;&lt;&lt;, _STATE_&gt;&gt;&lt;&lt; _POSTAL_&gt;&gt;&lt;&lt;</w:instrText></w:r><w:r><w:cr/><w:instrText>_COUNTRY_&gt;&gt;" \l 1033 \c 1 \e "United States" \d </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«AddressBlock»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00453714" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00453714"/>
    <w:p wsp:rsidR="00015B28" wsp:rsidRDefault="00453714" wsp:rsidP="00386D53"><w:r><w:fldChar w:fldCharType="begin"/></w:r><w:r><w:instrText> GREETINGLINE \f "&lt;&lt;_BEFORE_ Dear &gt;&gt;&lt;&lt;_TITLE0_&gt;&gt;&lt;&lt; _LAST0_&gt;&gt;
&lt;&lt;_AFTER_ ,&gt;&gt;" \l 1033 \e "Dear Sir or Madam," </w:instrText></w:r><w:r><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«GreetingLine»</w:t></w:r><w:r><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00453714" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00453714" wsp:rsidP="00386D53"/><w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"><w:r wsp:rsidRPr="006C08B9"><w:t>I would like to thank you for taking the time to meet with us. We were delighted to have the opportunity to discuss in more depth your needs and how our business unit can help you to fulfill them. As discussed, we will send you a detailed proposal soon and I will be in tou</w:t></w:r><w:r wsp:rsidR="005C0D45"><w:t>ch to schedule a follow-up meeting</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:t> next month.</w:t></w:r></w:p>
    <w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"/><w:p wsp:rsidR="006C08B9" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="006C08B9" wsp:rsidP="006C08B9"><w:r wsp:rsidRPr="006C08B9"><w:t>We look forward to the opportunity to work with you.</w:t></w:r></w:p>
    <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:pPr><w:ind w:left="720"/></w:pPr></w:p>
    <w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> AUTOTEXTLIST  \s Closing \t "Right-click here to select from a list of standard closing salutations." \* MERGEFORMAT </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:t>Sincerely,</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53" wsp:rsidP="00386D53"><w:pPr><w:ind w:left="720"/></w:pPr></w:p>
    <w:p wsp:rsidR="001B029C" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="001B029C"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_First_Name" </w:instrText></w:r><w:r wsp:rsidR="00670858"><w:instrText>\f " "</w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_First_Name» </w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Last_Name" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Last_Name»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:p wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9" wsp:rsidRDefault="00386D53"><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="begin"/></w:r><w:r wsp:rsidRPr="006C08B9"><w:instrText> MERGEFIELD "User_Title" </w:instrText></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="separate"/></w:r><w:r wsp:rsidR="004F592D"><w:rPr><w:noProof/></w:rPr><w:t>«User_Title»</w:t></w:r><w:r wsp:rsidRPr="006C08B9"><w:fldChar w:fldCharType="end"/></w:r></w:p><w:sectPr wsp:rsidR="00386D53" wsp:rsidRPr="006C08B9">
      <w:pgSz w:w="12240" w:h="15840"/><w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="720" w:footer="720" w:gutter="0"/><w:cols w:space="720"/><w:docGrid w:line-pitch="360"/></w:sectPr>
   </wx:sect>
 </w:body>
</w:wordDocument>

这个有什么想法吗?

编辑: 根据Gisha的建议,我尝试了下面的代码段并将我的XML字符串传递给内容。但它仍然会生成损坏的文件。

// How to: Create a new package as a Word document.
  public static void CreateNewWordDocument(string document)
  {
   using (WordprocessingDocument wordDoc =  WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
   {
    // Set the content of the document so that Word can open it.
    MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

     SetMainDocumentContent(mainPart);
    }
   }

    // Set content of MainDocumentPart.
    public static void SetMainDocumentContent(MainDocumentPart part)
    {
    const string docXml =
        @"<?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>";

     using (Stream stream = part.GetStream())
     {
         byte[] buf = (new UTF8Encoding()).GetBytes(docXml);
         stream.Write(buf, 0, buf.Length);
     }
    }

你的第一个XML片段是WordML(xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"),而不是OpenXml(xmlns:w=""http://schemas.openxmlformats.org/wordprocessingml/2006/main")- 你尝试过使用“Productivity Tool”和Open XML SDK吗? - Robert Muehsig
你尝试过将Body元素的OuterXml属性设置为你的Xml中Body元素的内容吗? - Mohamed Alikhan
1个回答

2
我尝试使用您在问题的编辑部分发布的代码,确实会产生一个损坏的文件。问题似乎与您在docXml字符串中指定的无效命名空间有关:
http://schemas.openxmlformats.org /wordprocessingml/2006/main

这应该改为:
http://schemas.openxmlformats.org/wordprocessingml/2006/main

“(中间没有空格)。这应该会生成一个可以无错误信息打开的文件。”
“然而,您发布的第一个XML具有不同的结构,需要不同的解决方案。我将尝试找到解决方案,并更新此回答以提供相关信息。”
“更新:”
“您发布的第一个XML(主节点为w:wordDocument)类似于本文所述的XML格式,并且似乎是Word 2003 XML格式,而不是Open XML:http://blogs.msdn.com/b/brian_jones/archive/2005/07/05/intro-to-word-xml-part-1-simple-word-document.aspx。”
“要使它正常工作,您可以将字符串保存在一个xml文件中:”
File.WriteAllText("C:\\data\\newFileName.xml", docXml);

然后,您可以使用Word打开该文件,而且应该能够顺利地读取它。然而,在这种情况下,您可能需要选择“打开方式...”选项,并手动选择Word来打开该文件,这相当不方便。
根据这篇文章(https://msdn.microsoft.com/en-us/library/office/aa223584%28v=office.11%29.aspx),您也应该能够将XML保存为doc文件,因此您可以像这样保存XML字符串:
File.WriteAllText("C:\\data\\newFileName.doc", docXml);

你发布的XML文件,这应该会生成一个可以在Word中打开而没有问题的文档文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接