<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>OpenXML</title>
        <link>http://geekswithblogs.net/EltonStoneman/category/9225.aspx</link>
        <description>The OpenXML format used in Office 2007,  and the .NET SDK</description>
        <language>en-GB</language>
        <copyright>EltonStoneman</copyright>
        <managingEditor>elton.stoneman@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <item>
            <title>XML Content Parts in the OpenXML SDK DocumentReflector</title>
            <link>http://geekswithblogs.net/EltonStoneman/archive/2008/12/04/xml-content-parts-in-the-openxml-sdk-documentreflector.aspx</link>
            <description>&lt;p style="text-align: center;"&gt;&lt;span style="font-size: 10pt;"&gt;[Source: &lt;a href="http://geekswithblogs.net/EltonStoneman"&gt;http://geekswithblogs.net/EltonStoneman&lt;/a&gt;] &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The latest release of the OpenXML SDK (&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&amp;amp;displaylang=en"&gt;v2.0 September 2008 CTP&lt;/a&gt;) comes with the DocumentReflector tool which can load an OpenXML document and reverse engineer the code for generating that document with the SDK. It's very handy but there's an issue with it when you have a Word document with content parts that contain XML. &lt;/p&gt;
&lt;p&gt;In this scenario, DocumentReflector will build the XML content for the part like this: &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;public&lt;/span&gt; 			&lt;span style="color: blue;"&gt;static&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; GenerateCustomXmlPart1(&lt;span style="color: rgb(43, 145, 175);"&gt;OpenXmlPart&lt;/span&gt; part) &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        { &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            System.IO.&lt;span style="color: rgb(43, 145, 175);"&gt;StreamWriter&lt;/span&gt; writer = &lt;span style="color: blue;"&gt;new&lt;/span&gt; System.IO.&lt;span style="color: rgb(43, 145, 175);"&gt;StreamWriter&lt;/span&gt;(part.GetStream()); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.Write(&lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;?xml version=\"1.0\" encoding=\"utf-16\"?&amp;gt;&amp;lt;RootNode&amp;gt;&amp;lt;/RootNode&amp;gt;"&lt;/span&gt;); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.Flush(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.Close(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        }&lt;/span&gt; 	&lt;/p&gt;
&lt;p&gt;The problem is that the stream doesn't contain a Unicode byte mark, so if you generate a document with this and then try to open it in DocumentReflector, you'll get an error when you navigate to the content part: "There is no Unicode byte order mark. Cannot switch to Unicode". If you generate the whole document using DocumentReflector's code, you'll have the same issue with the core file properties, and the document won't open in Word. &lt;/p&gt;
&lt;p&gt;It's a straightforward solution to use &lt;strong&gt;XmlWriter&lt;/strong&gt; instead of &lt;strong&gt;StreamWriter&lt;/strong&gt;: &lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: blue;"&gt;public&lt;/span&gt; 			&lt;span style="color: blue;"&gt;static&lt;/span&gt; 			&lt;span style="color: blue;"&gt;void&lt;/span&gt; GenerateCustomXmlPart1(&lt;span style="color: rgb(43, 145, 175);"&gt;OpenXmlPart&lt;/span&gt; part) &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        { &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt; 			&lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriter&lt;/span&gt; writer = &lt;span style="color: rgb(43, 145, 175);"&gt;XmlWriter&lt;/span&gt;.Create(part.GetStream()); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.WriteRaw(&lt;span style="color: rgb(163, 21, 21);"&gt;"&amp;lt;RootNode&amp;gt;&amp;lt;/RootNode&amp;gt;"&lt;/span&gt;); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.Flush(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;            writer.Close(); &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family: Courier New; font-size: 10pt;"&gt;        } &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The created document will now have the Unicode byte mark in the content parts, and will load correctly in Word and in DocumentReflector. Note that you need to remove the opening &lt;em&gt;&amp;lt;?xml…?&amp;gt;&lt;/em&gt; tag, as the &lt;strong&gt;XmlWriter&lt;/strong&gt; adds this for you. &lt;/p&gt;&lt;p&gt;&lt;a href="http://www.pheedo.com/click.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127566"&gt;&lt;img src="http://www.pheedo.com/img.phdo?x=6cda6ad746d942b9a1110d0715a4fa12&amp;u=127566" border="0"/&gt;&lt;/a&gt;&lt;/p&gt;&lt;iframe src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;PageID=31016&amp;amp;SiteID=1" width=1 height=1 Marginwidth=0 Marginheight=0 Hspace=0 Vspace=0 Frameborder=0 Scrolling=No&gt;
&lt;script language='javascript1.1' src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Browser=NETSCAPE4&amp;amp;NoCache=True&amp;PageID=31016&amp;amp;SiteID=1"&gt;&lt;/script&gt;
&lt;noscript&gt;&lt;a href="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Click&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" target="_blank"&gt;
&lt;img src="http://ads.geekswithblogs.net/a.aspx?ZoneID=5&amp;amp;Task=Get&amp;amp;Mode=HTML&amp;amp;SiteID=1&amp;amp;PageID=31016" width="1" height="1" border="0"  alt=""&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;/iframe&gt;
&lt;img src="http://geekswithblogs.net/EltonStoneman/aggbug/127566.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>EltonStoneman</dc:creator>
            <guid>http://geekswithblogs.net/EltonStoneman/archive/2008/12/04/xml-content-parts-in-the-openxml-sdk-documentreflector.aspx</guid>
            <pubDate>Thu, 04 Dec 2008 15:11:12 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/EltonStoneman/comments/127566.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/EltonStoneman/archive/2008/12/04/xml-content-parts-in-the-openxml-sdk-documentreflector.aspx#feedback</comments>
            <wfw:commentRss>http://geekswithblogs.net/EltonStoneman/comments/commentRss/127566.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>