Geeks With Blogs

News This is the *old* blog. The new one is at blog.sixeyed.com
Elton Stoneman
This is the *old* blog. The new one is at blog.sixeyed.com

[Source: http://geekswithblogs.net/EltonStoneman]

The latest release of the OpenXML SDK (v2.0 September 2008 CTP) 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.

In this scenario, DocumentReflector will build the XML content for the part like this:

public static void GenerateCustomXmlPart1(OpenXmlPart part)

{

System.IO.StreamWriter writer = new System.IO.StreamWriter(part.GetStream());

writer.Write("<?xml version=\"1.0\" encoding=\"utf-16\"?><RootNode></RootNode>");

writer.Flush();

writer.Close();

}

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.

It's a straightforward solution to use XmlWriter instead of StreamWriter:

public static void GenerateCustomXmlPart1(OpenXmlPart part)

{

XmlWriter writer = XmlWriter.Create(part.GetStream());

writer.WriteRaw("<RootNode></RootNode>");

writer.Flush();

writer.Close();

}

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 <?xml…?> tag, as the XmlWriter adds this for you.

Posted on Thursday, December 4, 2008 9:11 AM OpenXML | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: XML Content Parts in the OpenXML SDK DocumentReflector

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
This actually did not work for me.. The code still stays like this: public static void GenerateCustomXmlPart1(OpenXmlPart part)

{

System.IO.StreamWriter writer = new System.IO.StreamWriter(part.GetStream());

writer.Write("<?xml version=\"1.0\" encoding=\"utf-16\"?><RootNode></RootNode>");

writer.Flush();

writer.Close();

}

Any other ideas on how to change this?
Left by Digital Watches on Jan 29, 2010 5:05 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Now that’s the type of post I like to read.I am pretty much pleased with your good work.Thanks a lot for sharing.Keep up the good works.
Left by ecommerce-158 on Mar 24, 2010 10:24 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Nice post having excellent contents.I have been searching for this type of posts.I agree with most of what you are saying here.Thank you so much for sharing.Keep blogging.
Left by digital scrapbooking on Apr 10, 2010 2:17 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Your tips are extremely valuable.I am really very impress with your article.Thanks a lot for sharing.Keep blogging.
Left by appartamenti caorle on Apr 14, 2010 3:08 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Thanks for the enlightenment. Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!

Left by Villaggi Sardegna on May 11, 2010 6:13 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
This is a great site. Good fresh user interface and very informative blog.I appreciate the tips and the recommendation.Thanks for the share!Hope you post again soon.
Left by residence Toscana mare on May 29, 2010 4:11 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Thanks a lot for sharing this useful article with us.Really very nice article. Very detailed and step by step instruction for every aspirant to use it.
Left by tuscany agriturismo on May 30, 2010 7:26 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Thanks for an insightful post. These tips are really helpful. Again thanks for sharing your knowledge with us. Keep it up. Looking forward to reading your next post.
Left by skin care555 on Jun 12, 2010 7:48 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Thanks for this informative entry. I am forever searching for info on particular subjects.Keep blogging.
Left by colocation utah160 on Jul 26, 2010 3:38 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Microsoft published the Open XML Format SDK 2.0 defines a set of XML schemas for representing spreadsheets, charts,and word processing documents.It provides simple API that allows to create and manipulate Open XML documents.
Left by collagen on Nov 22, 2010 3:10 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I have go through your articles and found some information which is quite important for me. It is very informative.Looking forward for more interesting posts.
Left by unoccupied property insurance on Dec 03, 2010 3:27 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Every Open XML file is essentially a ZIP archive containing many other files. Office-specific data is stored in multiple XML files inside that archive. This is in direct contrast with the old WordML and SpreadsheetML formats which were single, non-compressed XML files.
Left by enigin on Feb 03, 2011 9:32 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Open XML formats are useful for developers because they are an open standard and are based on well-known technologies: ZIP and XML. The Open XML SDK 2.0 encapsulates many common tasks that developers perform on Open XML packages.
Left by article submission software on Mar 24, 2011 9:53 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I forget to remove opening xml tags. I have serious difficulties with this negligence.
Left by life experience degree on May 13, 2011 7:55 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Great read and good luck, we have added it to our bookmarks and will wait for a follow-up to this topic hopefully more positive ;-)
Indian escorts London
Left by London Escorts on Jun 05, 2011 12:03 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I feel good here, keeping share with friends .. .. now I want good things for all readers with some great links,
Left by san francisco giants replica per on Aug 20, 2011 2:29 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Thanks for all this useful info on your blog. Great articles
Left by Baumarkt & Garten Test on Oct 29, 2012 2:56 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I was browsing for this Lycium Barbarum for weight loss management; when I ran into this particular XML Content Parts in the OpenXML SDK DocumentReflector published in your internet-site. I did enjoy it and also I am intending in dropping in again.
Left by noellerc10 on Sep 27, 2013 10:05 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I'm not really commonly motivated by informative material, yet your take on XML Content Parts in the OpenXML SDK DocumentReflector really made me give some thought to your points of views. You have presented valuable and sound thoughts that happen to be reasonable and fascinating. I personally love to write about http://silverlakestudiola.com. And so I am aware of the time and effort it takes to form an informational article similar to this. Thanks a lot for featuring your nice work.
Left by Cesar Azevedo on Dec 19, 2013 9:01 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I'm not commonly empowered by educational posts, yet your topic on XML Content Parts in the OpenXML SDK DocumentReflector truly made me give thought to your points of views. You have unquestionably offered valuable and reliable views that happen to be rational and intriguing. I privately care to talk about Silver Lake Studio Hair Salon. So I am conscious of the hard work it will require to produce an educational write-up like this. Thank you so much for featuring your positive work.
Left by Cesar Azevedo on Dec 20, 2013 1:49 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
This is humiliating it had me quite a bit to figure out the whole concept of XML Content Parts in the OpenXML SDK DocumentReflector; so I attempted to Check this out once again.
Left by German Danley on Dec 27, 2013 8:09 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Hello folks! I was actually looking out for referencement naturel tutorials on the net but rather, I found this much more captivating XML Content Parts in the OpenXML SDK DocumentReflector; I feel I'll drop in here anew few days from now.
Left by Michael Beike on Jan 17, 2014 3:23 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Howdy fellas! Whilst going over XML Content Parts in the OpenXML SDK DocumentReflector, I met this word travail a domicile; to my inquisitiveness I scanned it up in the world wide web.
Left by Jonas Furst on Jan 20, 2014 2:13 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I have been really burnt out hunting for how to get out of credit card debt handbooks; so I looked for blogs just to rejuvenate my cognitive powers, and XML Content Parts in the OpenXML SDK DocumentReflector incredibly gave me more than enough, many thanks.
Left by Craig Higginbotham on Jan 30, 2014 3:19 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I've had a hard time seeking out Josh Groban musics in ITunes visit this site. Grateful I happen to go over a couple of compelling elements at your blog; Now I get higher potency.
Left by Randal Porter on Feb 20, 2014 2:30 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
I've had a laborious time looking out for Josh Groban songs on Youtube click here. Happy I was able to go through a few compelling aspects at your blog; At this time I get higher potency.
Left by Randal Porter on Feb 20, 2014 2:31 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
The meal recipes my colleague talks about amazes me; but the blog post on XML Content Parts in the OpenXML SDK DocumentReflector fairly brings home the bacon.
Left by iampatzy22 on Oct 17, 2014 11:23 AM

# re: XML Content Parts in the OpenXML SDK DocumentReflector
Requesting Gravatar...
Fantastic! XML Content Parts in the OpenXML SDK DocumentReflector taken my attention, I savored going through it along with hair removal at <a href="http://laserhairremovalat-home.com/'>http://laserhairremovalat-home.com/. Click here to know further more on it.
Left by Robert Way on Feb 19, 2015 10:08 AM

Your comment:
 (will show your gravatar)


Copyright © Elton Stoneman | Powered by: GeeksWithBlogs.net