gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:58:04 26 4
gpt4 key购买 nike

我正在从代码中读取一个元素,结果我得到了 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);
}
}

最佳答案

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

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

应该改为:

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

(中间没有空格)。这应该会生成一个可以打开而不会出现错误消息的文件。

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

更新

您发布的第一个 XML(以 w:wordDocument 作为主节点)与本文中描述的类似,似乎是 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,这应该会生成一个 doc 文件,该文件可以用 Word 毫无问题地打开。

关于c# - 如何将打开的xml字符串转换为word文档c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31152645/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com