none
How to use the Xml string returned from contentControl.Range.WordOpenXml property?

    Question

  • I have the aforesaid string with me which also contains inline images as a byte array. Essentially this string is the Open Xml representation of the contents of a rich text content control. I need to copy this data to an another rich text content control using Open Xml SDK 2.0. However I cannot load this string into a WordProcessingDocument object nor in a OpenXmlPackage object.

    The string looks something like this

    <pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage">
    
    <pkg:part
     pkg:name="/_rels/.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml" pkg:padding="512">...</pkg:part> 
    
    <pkg:part pkg:name="/word/_rels/document.xml.rels" pkg:contentType="application/vnd.openxmlformats-package.relationships+xml"
     pkg:padding="256">...</pkg:part> 
    
    <pkg:part pkg:name="/word/document.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml">...</pkg:part>
    
     <pkg:part pkg:name="/word/theme/theme1.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.theme+xml">...</pkg:part> 
    
    <pkg:part pkg:name="/word/media/image1.jpg" pkg:contentType="image/jpeg" pkg:compression="store">...</pkg:part> 
    
    <pkg:part pkg:name="/word/media/image2.jpg" pkg:contentType="image/jpeg" pkg:compression="store">...</pkg:part>
    
     <pkg:part pkg:name="/word/settings.xml"
    pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml"></pkg:part>
    
     <pkg:part pkg:name="/word/webSettings.xml"
    pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.webSettings+xml">...</pkg:part>
    
     <pkg:part pkg:name="/word/stylesWithEffects.xml" pkg:contentType="application/vnd.ms-word.stylesWithEffects+xml">...</pkg:part>
    
     <pkg:part pkg:name="/word/fontTable.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml">...</pkg:part>
    
     <pkg:part pkg:name="/word/styles.xml" pkg:contentType="application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml">...</pkg:part>
    
    </pkg:package>



    now after all the research I am not even sure if I would be able to copy the contents with such a representation. Please help.


    Rahul Lohia. Software Developer. Microsoft Technologies.


    • Edited by lohiarahul Friday, February 08, 2013 11:52 AM Spell error in the title
    Friday, February 08, 2013 11:52 AM

Answers

All replies

  • Hi Rahul

    You're using the Range.WordOpenXML command to get the XML, is that correct?

    This is in the "OPC flat file" format, meaning that the contents of the ZIP package have been "flattened" so that they can be represented in a string. In order to work with it with the Open XML SDK the flat file needs to be converted to a Zip package representation. See this article:

    http://blogs.msdn.com/b/ericwhite/archive/2008/09/29/transforming-flat-opc-format-to-open-xml-documents.aspx


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by lohiarahul Monday, February 11, 2013 10:59 AM
    Friday, February 08, 2013 3:55 PM
  • Hi,

    Thank you very much for the directions. Now I am able to get textual data out of the Xml string and append it to my destination content control. However, the images are still a problem. I have used the following code to copy the entire data to my content control. The sourceDoc is the OPC format achieved after the FlatToOPC() function you directed me to. Temporarily I have it saved as a docx and using it to retrieve data. Following is the code I use.

    using (WordprocessingDocument destinationDoc = WordprocessingDocument.Open(destination, true))
    		{ 
    		WordprocessingDocument sourceDoc = WordprocessingDocument.Open(source, true);
     
    		//Get the destination content control
    		SdtBlock cc = destinationDoc.MainDocumentPart.Document.Body.Descendants<SdtBlock>().FirstOrDefault();
    		DocumentFormat.OpenXml.OpenXmlElement xmlElement = default(DocumentFormat.OpenXml.OpenXmlElement);
    		xmlElement = cc.Descendants<SdtContentBlock>().FirstOrDefault();
    		OpenXmlElementList list = xmlElement.ChildElements;
    		OpenXmlElementList elementsToCopy = sourceDoc.MainDocumentPart.Document.Body.ChildElements;
    		List<OpenXmlElement> clonedElements = new List<OpenXmlElement>();
     
    		foreach (OpenXmlElement element in elementsToCopy)
    		{
    			clonedElements.Add((OpenXmlElement)element.Clone());
    		}
    		xmlElement.RemoveAllChildren();
    		xmlElement.Append(clonedElements);
                    }


    Rahul Lohia. Software Developer. Microsoft Technologies.

    Monday, February 11, 2013 9:20 AM
  • I used altChunk to read data from the file and insert into the content control. Works now. Thank you for all your help.

    Rahul Lohia. Software Developer. Microsoft Technologies.

    Monday, February 11, 2013 10:59 AM
  • Glad you got it working :-) Thanks for taking a moment to comment on that last little bit that made the difference.

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, February 11, 2013 12:35 PM
  • :) Thank you again.

    Rahul Lohia. Software Developer. Microsoft Technologies.

    Monday, February 11, 2013 12:40 PM