none
Populating Word document with XML using VisualStudio (C#) RRS feed

  • Question


  • Hi,

    I have a Word template which should be populated by an XML file using VisualStudio (preferably C#) because I want the final solution to be a '.dll' file which will be used in another C# application.

    I need to fill in many field in the word template through this (example) XML based on LOCALE criteria.  Look at this code as an example:

    <?xml version="1.0"?><br/>
    <fs:customer xmlns:fs="http://www.w3.org/2001/XMLSchema"> <br/>
      <fs:companyName>Futures Inc.</fs:companyName> <br/>
      <fs:contactName>Maria Anders</fs:contactName><br/>
      <fs:contactTitle>Sales Representative</fs:contactTitle><br/>
      <fs:phone>030-0074321</fs:phone><br/>
      <fs:fruit elementId='txtFruity'><br/>
      <fs:fruitType locale='EN'>Banana</fs:fruitType><br/>
      <fs:fruitType locale='ES'>Guineo</fs:fruitType><br/>
     </fs:fruit><br/>
      <fs:fruit elementId='elFruity'><br/>
      <fs:fruitType locale='EN'>Peach</fs:fruitType><br/>
      <fs:fruitType locale='ES'>Melocotón</fs:fruitType><br/>
     </fs:fruit><br/>
    </fs:customer><br/>
    
    

    The problem is with:

    i- finding the locale of the System and then

    ii- selecting the correct data node from the XML to

    iii- put in the Word template and save it.

     

    I have tried my luck with Content Controls and "Word 2007 Content Control Toolkit" using some of the following methods, but they don't work in my situation.

    http://seroter.wordpress.com/2009/12/23/populating-word-2007-templates-through-open-xml/

    http://www.c-sharpcorner.com/uploadfile/sudiptasdas/wordcshartxmlxslt03202006035329am/wordcshartxmlxslt.aspx

     

    • Changed type cesier Friday, November 19, 2010 1:31 PM
    Friday, November 19, 2010 9:10 AM

Answers

  • Hi cesier

    Do you need to interact with the user when you change the data in the Word file? Or is this something you could just as well change in the closed document (server-side, for example)?

    Please provide detail on HOW you're updating your XML. Are you exchanging the CustomXMLPart? Writing new values to the mapped nodes? Something else?

    <<and I don't know about any other option than Content Controls (CC) which can be mapped to an external XML file>>

    Please note that it's not possible to link content controls to an external XML file. The file must be part of the document's "zip package".

    It is possible to link to external XML information by inserting an IncludeText field into the document. This field includes three switches to support inclusion of XML information. Unfortunately, there's not a lot of documentation or examples around for this, but here are a couple of links

    http://office.microsoft.com/en-us/word-help/field-codes-includetext-field-HP005186165.aspx
    http://www.eggheadcafe.com/software/aspnet/29652605/includetext-xml.aspx


    Cindy Meister, VSTO/Word MVP
    Thursday, November 25, 2010 5:53 PM
    Moderator

All replies

  • Hi Cecier

    Well, for the first point you need to look at the CultureInfo class of the .NET Framework. For the second, you need to use the Framework's XML classes (XPath or possibly XSL).

    As to the rest of it, which would be on-topic for this forum, you need to better explain what it is you want to do with the information contained in your XML file. Just telling us the examples you've found "don't work" doesn't provide us with enough information. We need a more detailed scenario.


    Cindy Meister, VSTO/Word MVP
    Friday, November 19, 2010 7:51 PM
    Moderator
  • The main issue here is to put data from an XML file into a Word template.

    The data-holder in Word template, for the data from XML can be any thing e.g. Content Controls (and I don't know about any other option than Content Controls (CC) which can be mapped to an external XML file). Let's suppose, there is a CC in my Word template titled as 'txtFruity' and it should be mapped to the 'ES' locale of the above mentioned XML:

     

    <fs:fruit elementId='txtFruity'>
     <fs:fruitType locale='EN'>Banana</fs:fruitType>
     <fs:fruitType locale='ES'>Guineo</fs:fruitType>
    </fs:fruit>
    
    I tried using VBA from the word itself by loading the CustomXMLParts, the mapping is fine and it works but when I update my XML, it still refers to the old values. The VBA code:

    Public Sub cus()
    'Load the XML file
    
    ActiveDocument.CustomXMLParts.Add
    'ActiveDocument.CustomXMLParts(6).Load ("c:\TEMP\CustomerData.xml")
    
    Dim strXPath1 As String
    strXPath1 = "/Customer/fruit[@elementId='txtFruity']/fruitType[@locale='ES']"
    ActiveDocument.ContentControls(1).XMLMapping.SetMapping strXPath1
    
    Dim strXPath2 As String
    strXPath2 = "/Customer/ContactName"
    ActiveDocument.ContentControls(2).XMLMapping.SetMapping strXPath2
    
    Dim strXPath3 As String
    strXPath3 = "/Customer/ContactTitle"
    ActiveDocument.ContentControls(3).XMLMapping.SetMapping strXPath3
    
    Dim strXPath4 As String
    strXPath4 = "/Customer/fruit[@elementId='txtFruity']/fruitType[@locale='ES']"
    ActiveDocument.ContentControls(4).XMLMapping.SetMapping strXPath4
    
    Dim strXPath5 As String
    strXPath5 = "/Customer/fruit[@elementId='elFruity']/fruitType[@locale='EN']"
    ActiveDocument.ContentControls(5).XMLMapping.SetMapping strXPath5
    
    Dim strXPath6 As String
    strXPath6 = "/Customer/fruit[@elementId='txtFruity']/fruitType[@locale='ES']"
    ActiveDocument.ContentControls(6).XMLMapping.SetMapping strXPath6
    
    End Sub
    Thanks Cindy for the reply and I think this time I have described properly what I need.

    Monday, November 22, 2010 9:15 AM
  • Hi cesier

    Do you need to interact with the user when you change the data in the Word file? Or is this something you could just as well change in the closed document (server-side, for example)?

    Please provide detail on HOW you're updating your XML. Are you exchanging the CustomXMLPart? Writing new values to the mapped nodes? Something else?

    <<and I don't know about any other option than Content Controls (CC) which can be mapped to an external XML file>>

    Please note that it's not possible to link content controls to an external XML file. The file must be part of the document's "zip package".

    It is possible to link to external XML information by inserting an IncludeText field into the document. This field includes three switches to support inclusion of XML information. Unfortunately, there's not a lot of documentation or examples around for this, but here are a couple of links

    http://office.microsoft.com/en-us/word-help/field-codes-includetext-field-HP005186165.aspx
    http://www.eggheadcafe.com/software/aspnet/29652605/includetext-xml.aspx


    Cindy Meister, VSTO/Word MVP
    Thursday, November 25, 2010 5:53 PM
    Moderator