I am reading one element from code and I am getting Open XML string as a result.
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.
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 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 "<<_FIRST0_>><< _LAST0_>><< _SUFFIX0_>></w:instrText></w:r><w:r><w:cr/><w:instrText><<_STREET1_</w:instrText></w:r><w:r><w:cr/><w:instrText>>><<_STREET2_</w:instrText></w:r><w:r><w:cr/><w:instrText>>><<_CITY_>><<, _STATE_>><< _POSTAL_>><<</w:instrText></w:r><w:r><w:cr/><w:instrText>_COUNTRY_>>" \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 "<<_BEFORE_ Dear >><<_TITLE0_>><< _LAST0_>>
<<_AFTER_ ,>>" \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.
// 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);
}
}