wordpress hit counter
Re: Saving Custom XML files in a word document - WordprocessingML - Formats - OpenXML Developer

Re: Saving Custom XML files in a word document

Formats

Discussions about working with different Open XML Formats

Saving Custom XML files in a word document

  • rated by 0 users
  • This post has 7 Replies |
  • 0 Followers
  • Hello,

    I am Kristof Haerens, and I’m in my last year of civil engineering. This year, I have to make a Masters Thesis. For this project, I think that Office Open XML, and Custom XML in particular, may be the solution.

    For a little more info about the project, see my blog on: http://masterproefkristof.blogspot.com/

    The problem was that Office Open XML was new to me, so I had to do a lot of research first before I was able to start working on my project.

    Unfortunately, I have a problem with my code at this moment. I am now able to replace the custom xml file in a word document by using code in C#. The problem is that there has to be already a custom xml file (the item1 file) in the document, I cannot store a custom XML file in a document without custom xml yet.

    When I was searching for an answer to this problem, I found your article on Open Xml Developer:

    http://openxmldeveloper.org/archive/2006/08/05/463.aspx

    I think that this code may be the answer to this particular problem, but I am not able to get it working. I really hope that someone can help me out with this… It would help me a lot. I am a bit stuck on the coding at the moment :(


    This is my code to replace my custom xml file:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.IO;
    using System.IO.Packaging;

    namespace Programma
    {
        class Programma
        {
            static void Main(string[] args)
            {
                ReplaceCustomXml();
            }

            public static void ReplaceCustomXml()
            {

                //Load in the XML
                XmlDataDocument myXmlDocument = new XmlDataDocument();
                string fileName = @"C:\Users\Kristof\Desktop\Content Controls Mapping met C#\CustomerData.xml";
                myXmlDocument.Load(fileName);

                //Open the File using the packaging API
                using (Package p = Package.Open(@"C:\Users\Kristof\Desktop\Content Controls Mapping met C#\Origineel.docx", FileMode.Open, FileAccess.ReadWrite))
                {
                    Uri partUri = new Uri("/CustomXml/item1.xml", UriKind.Relative);
                    PackagePart part = p.GetPart(partUri);

                    //Overwrite the part with the new XML
                    using (Stream s = part.GetStream(FileMode.Create))
                    {
                        myXmlDocument.Save(s);
                    }

                    p.Flush();
                }
            }
        }
    }

    Could anyone help me out with the code to save a custom xml file and the properties file?

    Thanks a lot in advance!

    Looking forward to your answer,

    Kristof
  • There is a very nice tool called word 2007 content controls toolkit that adds custom xml parts for you. It also defines all the properties and relationships.

    Here is a link to an article that shows how to use it ( it also contains a link for downloading ):

    http://blogs.msdn.com/acoat/archive/2007/03/01/linking-word-2007-content-controls-to-custom-xml.aspx

  • If you are able (or want) to do it in Java rather than C#, here is how:

    http://dev.plutext.org/trac/docx4j/browser/trunk/docx4j/src/main/java/org/docx4j/samples/CreateDocxWithCustomXml.java

    cheers

    Jason
    docx4j maintainer
  • Gabriel,

    Thank you for your reply. I know about the toolkit. It is a nice tool indeed , but actually I want to do the very same, but with code in C#. Just like the toolkit, I need to add the custom xml part, and bind the nodes in the XML to the content controls in my word document.

    Jason,

    Thank you for your reply, but I want this to be done through C#-code.

    Can anyone help me out with this?

    Kristof
  • Will this be helpful?

    If yes, please let us know, thanks!

  • goodol:

    Will this be helpful?

    If yes, please let us know, thanks!



    Goodol,

    Thank you for your reply! However, the website doesn't provide me the code I need. Zeyad Rajabi talks about the three main ways to specify content control binding:

      1. This addin to Word makes binding content controls to XML data easy and intuitive. A must have tool
    1. Use the Word object model
      1. You can use ContentControl.XMLMapping.SetMapping()
    2. Directly manipulate the underlying xml
      1. You can use the SDK to help out here

    The first way, the content control tool kit, can be used, but I need the exact code.
    The second way is in VB. I tried that out and it works, but I've got another problem with that, that I will probably explain in another thread if I don't find the solution. I need the code in C#, using the SDK.

    Can someone help me out with this? I would be very greatful.
  • Hi Kristof,

    For questions like these I would recommend to check out the Open XML Document Reflector tool that comes with the Open XML SDK. You can create a document with bound content controls using any of the methods you mentioned above. Once you have a document you can then open it up in this tool to check out code that can be used to generate that document. That being said, here is how you can accomplish your task using the SDK:

    First you need to add a custom xml part and then feed it the memory stream that represents the xml for that part

    CustomXmlPart custompart = mainPart.AddNewPart<CustomXmlPart>();

    custompart.FeedData(memorystream);

    Now that you have a part in the package you need to have a content control that is bound. Take a look at the following code as an example.

    SdtRun sdtr = new SdtRun(

          new SdtProperties(

          ...

          new DataBinding(){ PrefixMappings = "xmlns:ns0=\'http://contoso.com/2005/contracts/commercialSale\' ", XPath = "/ns0:contract[1]/ns0:dateExecuted[1]", StoreItemId = "{ABB284D9-2C5E-41BD-A2F2-B5FC934955A9}" },

          ...

    Hope this helps,

    Zeyad Rajabi

  • Zeyad Rajabi,

    Thanks a lot for your reply! The document reflector tool is very usefull indeed. Thanks to your help, and thanks to an article on Eric White's Blog (link below) I was able to resolve my problem. I was on your blog (microsoft office word team) and Eric's blog already multiple times. There are a lot of articles that helped me out with  different problems. You guys really do a great job! Thanks!

    http://blogs.msdn.com/ericwhite/archive/2008/10/19/creating-data-bound-content-controls-using-the-open-xml-sdk-and-linq-to-xml.aspx


Page 1 of 1 (8 items)