none
Manipulating CustomXmlPart without opening the document RRS feed

  • Question

  • Hi everyone,

    I wrote an AddIn that attaches a customXmlPart to docx documents. I am now trying to write a second addIn that is able to read out those parts and compare them.

    I am getting all .docx files a user wants to compare with the following code snippet:

    DirectoryInfo di = new DirectoryInfo(path);
    
    foreach (FileInfo f in di.GetFiles("*.docx"))
    {
    }

    Now my problem is the following: how do I get to read out the customXmlParts of those files without opening them in word? I cannot even figure out how to "load" those files into document objects.

    Any help is greatly appreciated!

    Monday, April 1, 2013 4:44 AM

Answers

  • Hi Rinmic

    Sorry it's taken me so long to get back to this, but things here have been hectic...

    Here are a couple of code snippets that show how to 1) get the CustomXMLParts in a given document and 2) get the XML from a specific CXP.

    The code is attached to 1) a button on a WinForm and 2) a listbox showing the list generated by (1)

    private void btnCustomXMLParts_Click(object sender, EventArgs e)
    {
        string docPath = @"C:\Test\cxpFlower.docx";
        using (WordprocessingDocument pkg = WordprocessingDocument.Open(docPath, false))
        {
            MainDocumentPart docPart = pkg.MainDocumentPart;
            IEnumerable<CustomXmlPart> cxps = docPart.GetPartsOfType<CustomXmlPart>();
            if (cxps != null)
            {
                listBox1.Visible = true;
                foreach (CustomXmlPart cxp in cxps)
                {
                    this.listBox1.Items.Add(cxp.Uri.ToString());
                }
            }
        }
    }
    
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
            string docPath = @"C:\Test\cxpFlower.docx";
            using (WordprocessingDocument pkg = WordprocessingDocument.Open(docPath, false))
            {
                MainDocumentPart docPart = pkg.MainDocumentPart;
                CustomXmlPart cxp = docPart.GetPartsOfType<CustomXmlPart>().Where(c => c.Uri.ToString() == listBox1.SelectedItem.ToString()).FirstOrDefault();
                StreamReader r = new StreamReader(cxp.GetStream(FileMode.Open, FileAccess.Read));
                txtMessages.Text = r.ReadToEnd().ToString();
            }
            listBox1.Items.Clear();
            listBox1.Visible = false;
    }


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Monday, April 8, 2013 9:14 AM
    Monday, April 8, 2013 8:48 AM
    Moderator

All replies

  • Hi Rinmic

    You need to work with the Zip Package containing the XML files that make up a document. For this, System.IO.Packaging and standard XML namespaces are required. To make it easier, you could use the OPen XML SDK. So I'm going to move this message to the Open XML SDK forum.

    For more about the Office Open XML file formats and the Open XML SDK start here: OpenXMLDeveloper.org


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, April 1, 2013 3:59 PM
    Moderator
  • Thanks Cindy, I was afraid I need to manually open the Zip Package :). Maybe someone in the XML SDK forum can give me some pointers on how to use the Open XML SDK.
    Monday, April 1, 2013 7:27 PM
  • Hi Rinmic

    Sorry it's taken me so long to get back to this, but things here have been hectic...

    Here are a couple of code snippets that show how to 1) get the CustomXMLParts in a given document and 2) get the XML from a specific CXP.

    The code is attached to 1) a button on a WinForm and 2) a listbox showing the list generated by (1)

    private void btnCustomXMLParts_Click(object sender, EventArgs e)
    {
        string docPath = @"C:\Test\cxpFlower.docx";
        using (WordprocessingDocument pkg = WordprocessingDocument.Open(docPath, false))
        {
            MainDocumentPart docPart = pkg.MainDocumentPart;
            IEnumerable<CustomXmlPart> cxps = docPart.GetPartsOfType<CustomXmlPart>();
            if (cxps != null)
            {
                listBox1.Visible = true;
                foreach (CustomXmlPart cxp in cxps)
                {
                    this.listBox1.Items.Add(cxp.Uri.ToString());
                }
            }
        }
    }
    
    private void listBox1_DoubleClick(object sender, EventArgs e)
    {
            string docPath = @"C:\Test\cxpFlower.docx";
            using (WordprocessingDocument pkg = WordprocessingDocument.Open(docPath, false))
            {
                MainDocumentPart docPart = pkg.MainDocumentPart;
                CustomXmlPart cxp = docPart.GetPartsOfType<CustomXmlPart>().Where(c => c.Uri.ToString() == listBox1.SelectedItem.ToString()).FirstOrDefault();
                StreamReader r = new StreamReader(cxp.GetStream(FileMode.Open, FileAccess.Read));
                txtMessages.Text = r.ReadToEnd().ToString();
            }
            listBox1.Items.Clear();
            listBox1.Visible = false;
    }


    Cindy Meister, VSTO/Word MVP, my blog

    • Marked as answer by Rinmic Monday, April 8, 2013 9:14 AM
    Monday, April 8, 2013 8:48 AM
    Moderator
  • Hi Cindy,

    thanks for the reply! I should have stated in this thread that I used the Open XML SDK with great success. It did everything I needed it to do. I am now in the process of building a graph using the xml information, with Quickgraph and visualizing it with graph#.  Lots of new stuff to learn :).

    Thanks for all the help,

    Rinmic

    Monday, April 8, 2013 9:14 AM