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(
                        mainDocPart.NameTable);
            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.