Elton Stoneman

  Home  |   Contact  |   Syndication    |   Login
  120 Posts | 0 Stories | 3607 Comments | 0 Trackbacks

News

Archives

Post Categories

[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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati
posted on Thursday, December 04, 2008 9:11 AM

Feedback

# re: XML Content Parts in the OpenXML SDK DocumentReflector 1/29/2010 5:05 AM Digital Watches
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?

# re: XML Content Parts in the OpenXML SDK DocumentReflector 3/24/2010 10:24 AM ecommerce-158
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 4/10/2010 2:17 AM digital scrapbooking
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.

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

# re: XML Content Parts in the OpenXML SDK DocumentReflector 5/11/2010 6:13 AM Villaggi Sardegna
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!



# re: XML Content Parts in the OpenXML SDK DocumentReflector 5/29/2010 4:11 AM residence Toscana mare
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 5/30/2010 7:26 AM tuscany agriturismo
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 6/12/2010 7:48 AM skin care555
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.


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

# re: XML Content Parts in the OpenXML SDK DocumentReflector 11/22/2010 3:10 AM collagen
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 12/3/2010 3:27 AM unoccupied property insurance
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 2/3/2011 9:32 AM enigin
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 3/24/2011 9:53 AM article submission software
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.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 5/13/2011 7:55 AM life experience degree
I forget to remove opening xml tags. I have serious difficulties with this negligence.

# re: XML Content Parts in the OpenXML SDK DocumentReflector 6/5/2011 12:03 AM London Escorts
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

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

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: