wordpress hit counter
WordProcessingML Snippets: Part 1, utility snippets - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

WordProcessingML Snippets: Part 1, utility snippets

WordProcessingML Snippets: Part 1, utility snippets

  • Comments 12


This article is part of the "Building documents with code snippets" series .

This first article on working with the WordProcessingML snippets will go into the details of three snippets you can use to form a basic framework for handling WordProcessingML files. They provide basic plumbing code which you will need to perform the most common action, getting to the <w:body> node. The first snippet gets you to the main document part. The other two help you access this node, which is where you will need to be to modify the main body of a document.

The first snippet is already supplied with the OpenXml snippet download, it's the "HowToGetToDocPart" snippet. This snippet will be used to crack open a WordProcessingML file and get to the main document part.
When you use the snippet, you'll find the following code section inside the 'GetToDocPart' method:         

        XmlDocument doc = new XmlDocument();
        doc.Load(documentPart.GetStream(FileMode.Open, FileAccess.Read));
        //  =============================
        //  Your code, which works with the document part, goes here.
        //  =============================



In the upcoming series of articles we will add code to this section to perform actions such as handling tables or paragraphs.

If you have opened up any OpenXml file and poked around a little, you may have noticed various Xml namespaces that are used inside the document. If you want to use X-Path to query parts of this document, you will need to supply these namespaces with your queries. The System.Xml library uses an XmlNamespaceManager object to store this namespace information, and that's where the second snippet comes in. It creates a new instance of the XmlNamespaceManager class, filled up with common namespaces used by WordProcessingML.
The following code will be added to your file when you use this snippet. The 'mainDocPart' parameter receives its value using the code from the first snippet. (The code has been abbreviated a bit)

        static XmlNamespaceManager CreateNamespaceManager(
            XmlDocument mainDocPart)
            XmlNamespaceManager manager = new XmlNamespaceManager(
            manager.AddNamespace("ve", "http://schemas....
            manager.AddNamespace("ds", "http://schemas...
            return manager;


The third and final snippet in this overview is used to give you access to the XmlNode representing the main body of a WordProcessingML document. This is where we will need to be to modify the body of the document. To query the XmlDocument for this node the code requires the XmlNamespaceManager created by the second snippet.

The code is really basic:

        static XmlNode GetToBodyNode(
            XmlDocument document, XmlNamespaceManager namespaces)
            return document.SelectSingleNode("//w:body", namespaces);


You can use these two snippets inside the code block inserted by the first snippet:


        //  =============================
        //  Your code, which works with the document part, goes here.
        //  =============================

        XmlNamespaceManager namespaces = CreateNamespaceManager(doc);
        XmlNode bodyNode = GetToBodyNode(doc, namespaces);

The other WordProcessingML articles in this series will append code right after these two lines. The next article will go into working with paragraphs, covering two new snippets: AddParagraph and RemoveParagraph.  Expect that to be posted in the next few days.

Page 1 of 1 (12 items)