如何将打开的xml字符串转换为word文档c#

时间:2022-10-30 15:36:36

I am reading one element from code and I am getting Open XML string as a result.

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

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

Now, I want to convert this string in memorystream so that it can be read by WordProcessingDocument as below.

现在,我想在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());
           }      
  }

When I try above code, it could not open stream and raises error of corrupted data.

当我尝试上面的代码时,它无法打开流并导致数据损坏。

XML string looks like :

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>

Any idea on this?

任何想法吗?

Edits : As Per Gisha's suggestion, I have tried below code snippet and passed my xml string to content. Still it generates corrupted file.

编辑:根据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);
     }
    }

1 个解决方案

#1


2  

I've tried to use the code You posted in the Edits section of Your question and it produces a corrupted file indeed. The issue seems to be associated with an invalid namespace You have specified in the docXml string:

我已经尝试使用您在问题的编辑部分中发布的代码,它确实产生了一个损坏的文件。这个问题似乎与您在docXml字符串中指定的一个无效名称空间有关:

http://schemas.openxmlformats.org /wordprocessingml/2006/main

that should be changed to:

应将其改为:

http://schemas.openxmlformats.org/wordprocessingml/2006/main

(without a whitespace in the middle). This should produce a file that can be opened without error messages.

(中间没有空格)。这将产生一个可以在没有错误消息的情况下打开的文件。

The first XML You posted, however, has got a different structure and requires a different solution. I'll try to find a solution and update this answer with some information about it.

然而,您发布的第一个XML具有不同的结构,需要不同的解决方案。我将尝试找到一个解决方案,并更新这个答案的一些信息。

UPDATE

更新

The first XML You've posted (with w:wordDocument as main node) is similar to the one described in this article and seems to be a Word 2003 XML format rather than Open XML: http://blogs.msdn.com/b/brian_jones/archive/2005/07/05/intro-to-word-xml-part-1-simple-word-document.aspx.

您发布的第一个XML(以w:wordDocument为主节点)与本文描述的XML格式相似,似乎是Word 2003 XML格式,而不是Open XML: http://blogs.msdn.com/b/brian_jones/archive/2005/07/introtoword - XML -part- simple- document.aspx。

To make it work, You can save the string in an xml file:

要使其工作,可以将字符串保存在xml文件中:

File.WriteAllText("C:\\data\\newFileName.xml", docXml);

Then, You can open the file with Word and it should be able to read it without issues. However, in this case You will probably have to select Open with... option and manually choose Word to open the file, which is quite inconvenient.

然后,您可以用Word打开文件,它应该能够毫无问题地读取它。然而,在这种情况下,您可能不得不选择Open with…选择并手动选择Word打开文件,这很不方便。

According to this article (https://msdn.microsoft.com/en-us/library/office/aa223584%28v=office.11%29.aspx), however, You should be able to save the XML as doc file as well, so You can save the XML string like this:

根据本文(https://msdn.microsoft.com/en-us/library/office/aa223584%28v=office.11%29.aspx),但是,您也应该能够将XML保存为doc文件,这样就可以保存XML字符串:

File.WriteAllText("C:\\data\\newFileName.doc", docXml);

For the XML You've posted, this should produce a doc file that can be opened by Word without issues.

对于您所发布的XML,应该生成一个文档文件,该文件可以通过Word打开而不会出现问题。

#1


2  

I've tried to use the code You posted in the Edits section of Your question and it produces a corrupted file indeed. The issue seems to be associated with an invalid namespace You have specified in the docXml string:

我已经尝试使用您在问题的编辑部分中发布的代码,它确实产生了一个损坏的文件。这个问题似乎与您在docXml字符串中指定的一个无效名称空间有关:

http://schemas.openxmlformats.org /wordprocessingml/2006/main

that should be changed to:

应将其改为:

http://schemas.openxmlformats.org/wordprocessingml/2006/main

(without a whitespace in the middle). This should produce a file that can be opened without error messages.

(中间没有空格)。这将产生一个可以在没有错误消息的情况下打开的文件。

The first XML You posted, however, has got a different structure and requires a different solution. I'll try to find a solution and update this answer with some information about it.

然而,您发布的第一个XML具有不同的结构,需要不同的解决方案。我将尝试找到一个解决方案,并更新这个答案的一些信息。

UPDATE

更新

The first XML You've posted (with w:wordDocument as main node) is similar to the one described in this article and seems to be a Word 2003 XML format rather than Open XML: http://blogs.msdn.com/b/brian_jones/archive/2005/07/05/intro-to-word-xml-part-1-simple-word-document.aspx.

您发布的第一个XML(以w:wordDocument为主节点)与本文描述的XML格式相似,似乎是Word 2003 XML格式,而不是Open XML: http://blogs.msdn.com/b/brian_jones/archive/2005/07/introtoword - XML -part- simple- document.aspx。

To make it work, You can save the string in an xml file:

要使其工作,可以将字符串保存在xml文件中:

File.WriteAllText("C:\\data\\newFileName.xml", docXml);

Then, You can open the file with Word and it should be able to read it without issues. However, in this case You will probably have to select Open with... option and manually choose Word to open the file, which is quite inconvenient.

然后,您可以用Word打开文件,它应该能够毫无问题地读取它。然而,在这种情况下,您可能不得不选择Open with…选择并手动选择Word打开文件,这很不方便。

According to this article (https://msdn.microsoft.com/en-us/library/office/aa223584%28v=office.11%29.aspx), however, You should be able to save the XML as doc file as well, so You can save the XML string like this:

根据本文(https://msdn.microsoft.com/en-us/library/office/aa223584%28v=office.11%29.aspx),但是,您也应该能够将XML保存为doc文件,这样就可以保存XML字符串:

File.WriteAllText("C:\\data\\newFileName.doc", docXml);

For the XML You've posted, this should produce a doc file that can be opened by Word without issues.

对于您所发布的XML,应该生成一个文档文件,该文件可以通过Word打开而不会出现问题。