none
XML Roundtrip Solution with VSTO RRS feed

  • Question

  • Hello All,

    I need to be able to open XML files that follow my custom XML Schema in Word 2010, edit the contents in Word, (validate with my Schema if possible) and save it back as XML again. The environment is VS2010, VSTO, C#, Word 2010.

    There are two solutions I can think of at present:

    1. Using Open XML SDK, traverse the directory containing my XML files and convert them to ".docx" formats. These can then be  opened in Word for editing. After editing save them back as ".docx" and convert them to my custom XML with the Open XML SDK again.  I can use a document-level customization(template with customized ribbons and macros) with VSTO and use it for the editing in Word.
    2. Create a VSTO add-in for converting my XML to WordML "on-the-fly" when it is "imported" into WORD. The add-in can open my customized template for editing. I could then have an "export" button to validate and save the contents after editing back to my XML.

    Any other ideas are most welcome. Which would be the efficient way to proceed?

    Thanks in advance for your suggestions.


    Greets

     

    Monday, May 30, 2011 8:52 AM

Answers

  • Hi Sam

    <<for importing / opening XML documents, we could use content controls mapped to custom XML documents.>>

    Custom XML PARTS

    <<But if I want to edit certain nodes such as adding new nodes within the document as allowed by the schema, basically using Word to edit raw XML nodes (UI based edit), then it is not possible when we use content controls>>

    It should be possible. You can change the Custom XML Part using the object model to add (or delete) nodes if you need new ones. After you do that, insert content controls for the nodes and link them to the corresponding nodes in the CXP.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by samwillie Sunday, June 26, 2011 6:33 PM
    Wednesday, June 22, 2011 1:08 PM
    Moderator

All replies

  • Hi sam

    I don't understand why these need to be opened in Word? Can you elaborate a bit on that so that we can better understand your requirements?


    Cindy Meister, VSTO/Word MVP
    Monday, May 30, 2011 10:17 AM
    Moderator
  • Hi Cindy,

    The requirement is as follows. The input XML is automatically generated from a native XML database. The XML data needs to be enriched with more information such as adding description, cross references, toc etc.. It would be easier for my users to be able to edit (add/modify/delete) this content in Word, rather than working with raw XML data. After editing these can then be saved as enriched XML data, back again to the database. The XML database follows a custom XML Schema that I would need to validate with in Word before saving the content back as XML.

    Hope this helps. Please let me know if you need further information.

    Greets,

    Monday, May 30, 2011 6:48 PM
  • It seems to me that using Word to edit and save an XML document might be more pain than gain. Why not edit the document with Notepad, or better, Notepad++? You would avoid the risk of Word potentially inserting things in to your document, while getting some help validating properly closed tags.
    Tuesday, May 31, 2011 2:04 AM
  • Hi Sam

    Thanks for the additional info...

    <<The XML database follows a custom XML Schema that I would need to validate with in Word before saving the content back as XML.>>

    Word has no facility for this part, but I assume it wouldn't be a problem for you to first "convert" the Word content to your basic XML, then run the validation before writing back to the database.

    If this step requires interaction with the user, then it seems to me the second approach outlined in your first message may be the better one?


    Cindy Meister, VSTO/Word MVP
    Tuesday, May 31, 2011 9:17 AM
    Moderator
  • Hi Mathias, Cindy,

    Thanks for your prompt responses.

    Mathias, I agree. However Word would make a better fit, in which case the XML content can be rendered in chapters with table of contents and subsequently exported as PDF.

    Cindy, if I understand you correctly, I will not be able to validate the XML content (after editing) within Word, is that right? If this is the case, it is definitely not an issue to save from Word as XML and then validate it with some other tool, before saving to database.

    So, my second approach would now read:

    Create a VSTO add-in for converting my XML to WordML "on-the-fly" when it is "imported" into WORD. The add-in can open my customized template for editing. I could then have an "export" button to save the contents back to my XML.

    Thanks Cindy, this answers my question.

    On the same note then, would it be better to have the following two customizations, for this solution?

    1. One application-level addin for handling the import and export events (trigged by buttons in ribbon)
    2. One document level customization for the template itself, that the add-in needs to open for editing.

    Thanks again,

    Greets

     

    Tuesday, May 31, 2011 8:43 PM
  • Hi Sam

    <<Cindy, if I understand you correctly, I will not be able to validate the XML content (after editing) within Word, is that right?>>

    Not as part of the Word object model, or within the context of the Document object.

    But you could (for example) use the Range.XML or Range.WordOpenXML property to pick up any part of (or all of) the content and validate it in memory - without needing to close the document.

    << The add-in can open my customized template for editing.>>

    I'm not quite clear on why you need a (VSTO) template for editing? I can imagine reasons, but I want to be sure I understand your thought processes before I offer an opinion on this one :-)


    Cindy Meister, VSTO/Word MVP
    Wednesday, June 1, 2011 5:39 AM
    Moderator
  • Hi Cindy,

    Nice tip, with the Range.XML property. Will check this.

    Regarding the customized template - one usecase is that, there are placeholders/content-controls for document properties within headers and footer sections in the document. Users can click a button on the ribbon which opens up a dialog box with text fields and fill in the properties and update all the headers and footers. Other reasons are just to show or hide certain tabs/groups/controls in the ribbon.

     

    Greets,

     

    Thursday, June 2, 2011 4:56 PM
  • Hi Sam

    Why couldn't you include these elements in the Add-in Ribbon? Just change the Visible or Enabled attribute when in a document that needs them?

    The other is certainly possible, it just seems to me this would complicate matters (deployment, if the user wants to work in multiple documents with your XML tool, etc.)


    Cindy Meister, VSTO/Word MVP
    Thursday, June 2, 2011 5:21 PM
    Moderator
  • Hi Cindy,

    Thanks much. Your idea definitely sounds less complicated. I want to make sure that I understand you right. Please correct me if I got it wrong.

    For an efficient solution:
    1. There is no need for (VSTO) template. In other words, no need for a macro enabled template. A custom template with layout customization and place-holders for document properties woud suffice.
    2. When the user opens this template or creates documents based on this template the Add-in tab in the Ribbon becomes visible with a custom group containing controls for the following:
        Importing the XML data
        Exporting the content
        Control for filling up document properties in template
        Hiding certain built-in controls in the Word Ribbon
        ---- and so on..

    This would mean:

    1. The solution just requires one application level add-in, making the deployment easier.
    2. The user can use the normal built-in Word ribbon controls and tabs when he works with other documents not based on this template.
    3. Import and Export is only done to and from this custom template or documents based on this template. (This is also what I need).

    Right?

    Greets,

    Friday, June 3, 2011 10:18 AM
  • Hi Sam

    <<1. There is no need for (VSTO) template. In other words, no need for a macro enabled template. A custom template with layout customization and place-holders for document properties woud suffice.>>

    As best I can judge, given the information I have, that is correct.

    <<2. When the user opens this template or creates documents based on this template the Add-in tab in the Ribbon becomes visible >>

    Yes. Note that your code needs to monitor various events (DocumentChange, NewDocument, etc.) of the Word application in order to set the Visible (or Enabled) properties (attributes if this is Ribbon XML) of the controls. I'm thinking along the lines of your having a Ribbon tab and in that tab, one Group with controls that are always available (Import) and other groups where the controls are only available when working in a special document.

    If the user isn't supposed to use any other commands when working in such a document, then your Ribbon definition is going to be a bit complexer as you'll need to also control the Visible/Enable of the built-in tabs, as well...

    <<1. The solution just requires one application level add-in, making the deployment easier.
    2. The user can use the normal built-in Word ribbon controls and tabs when he works with other documents not based on this template.
    3. Import and Export is only done to and from this custom template or documents based on this template. (This is also what I need).>>

    1. ...and the template, of course

    2. Yes...

    3. Your code needs to set that up, but, yes, it's possible to do it like this


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by samwillie Sunday, June 5, 2011 7:46 PM
    • Unmarked as answer by samwillie Friday, June 10, 2011 9:58 AM
    Friday, June 3, 2011 3:46 PM
    Moderator
  • Hi Cindy,

    Great! Thanks much for your time and the informative replies :-)

    Greets,

    Sunday, June 5, 2011 7:46 PM
  • Hi Cindy,

    I have a question on the discussion we had here and didn't want to start a new thread. So, sorry for posting again in an already "marked as answer" thread.

    The question is regarding the Import and Export. For importing XML to WordML, I could use LINQ. For the export, I noticed that once we have the imported XML content into Word as WordML( Word document), we could mark the content with XML tags based on the Schema Structure. Then use the Word 2003 XML Document (*.xml) with  the "Save data only" option to save it back as the original XML. This sounds easier than writing a functional transformation from WordML back to XML. Is this the correct approach?

    Thanks in advance,

    Greets
    Friday, June 10, 2011 10:01 AM
  • HI Sam

    If I'm understanding you correctly, the technology to which you refer (XML tags) has been removed from Word, due to a court decision made a few years ago that has been upheld by the U.S. Supreme Court. I think it would be a very bad idea to try to base any solution on that technology...


    Cindy Meister, VSTO/Word MVP
    Thursday, June 16, 2011 12:10 PM
    Moderator
  • Hi Cindy,

    I probably didn't explain it clearly.

    In Word 2010, I am able to tag/mark my content with the XML tags in the Schema and save it as raw XML.(datacentric). This feature is still available, and has not been removed. My understanding was that Microsoft removed only the questionable feature in the 2010 versions and left the other features untouched.

    If this feature can be used, we could use the "save data only" option to save as XML, right?

    Thanks in advance for your reply,

    -sam

     

     

    Friday, June 17, 2011 8:14 AM
  • I probably didn't explain it clearly.

    In Word 2010, I am able to tag/mark my content with the XML tags in the Schema and save it as raw XML.(datacentric). This feature is still available, and has not been removed. My understanding was that Microsoft removed only the questionable feature in the 2010 versions and left the other features untouched.

    If this feature can be used, we could use the "save data only" option to save as XML, right?

    Hi Sam

    I think we're talking about the same thing, but understanding it differently. But I could be wrong...

    MS left that little bit of the UI in-place, possibly in the hopes that they'd win the last appeal before the Supreme Court. But they didn't. If you want this to work a few years into the future then I don't think it's a good idea to use your proposed approach.

    It appears, from what you've told us, that the users won't be applying formatting, just editing content? If that's the case, use plain text content controls instead of XML Nodes. Link the controls to a Custom XML Part in the document that conforms to your schema.


    Cindy Meister, VSTO/Word MVP
    Friday, June 17, 2011 11:58 AM
    Moderator
  • Hi Cindy,

    Thanks much. This clarifies the confusion :-) I was under the impression that I could add XML nodes wherever necessary from the XML Structure tree on the right hand side, as part of editing the XML content within Word, before saving.  For example, if I want to tag some content with an element which is allowed inside in a parent element, then this may not be possible now, isn't it?

    Greets,

    Monday, June 20, 2011 9:13 AM
  • Thanks much. This clarifies the confusion :-) I was under the impression that I could add XML nodes wherever necessary from the XML Structure tree on the right hand side, as part of editing the XML content within Word, before saving.  For example, if I want to tag some content with an element which is allowed inside in a parent element, then this may not be possible now, isn't it?

    Hi Sam

    It's physically still possible, but probably won't be in a future version. And if the file is ever opened in Word again, those XML nodes in the document will be gone.


    Cindy Meister, VSTO/Word MVP
    Monday, June 20, 2011 10:03 AM
    Moderator
  • Hi Cindy,

    Finally, to wrap this up - if we were to adhere to a right extensible approach based on my requirements, this means that for importing / opening XML documents, we could use content controls mapped to custom XML documents. And for Export, I could just save the data as XML. But if I want to edit certain nodes such as adding new nodes within the document as allowed by the schema, basically using Word to edit raw XML nodes (UI based edit), then it is not possible when we use content controls, right, atleast for now?

    Thanks for your patience,

    Monday, June 20, 2011 11:25 AM
  • Hi Sam

    <<for importing / opening XML documents, we could use content controls mapped to custom XML documents.>>

    Custom XML PARTS

    <<But if I want to edit certain nodes such as adding new nodes within the document as allowed by the schema, basically using Word to edit raw XML nodes (UI based edit), then it is not possible when we use content controls>>

    It should be possible. You can change the Custom XML Part using the object model to add (or delete) nodes if you need new ones. After you do that, insert content controls for the nodes and link them to the corresponding nodes in the CXP.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by samwillie Sunday, June 26, 2011 6:33 PM
    Wednesday, June 22, 2011 1:08 PM
    Moderator
  • Hi Cindy,

    Thanks much!

    Greets

    Sunday, June 26, 2011 6:33 PM