none
How to get the value of a content control in word document using XML RRS feed

  • Question

  • Hi everyone,

    I'm a newbie with content controls and word XML format. I'm trying to get the value of the content controls within a Word 2007 document by parsing it's XML using Open XML SDK. Currently, I'm retrieving the w:sdtContent tag of the content controls to be able to get the value. Then I get all the w:r tags and look for the value. I'm not sure if this is the correct path to do it. Is there already an API that will do this task? It would really help me if I somebody can provide a snippet on how to correctly do this. I tried searching the net but haven't found one.


    Many thanks in advance!
    Sunday, April 18, 2010 2:04 AM

Answers

  • By the way, if you are using OpenXML SDK 2.0, you can use the following codes to retrieve it,

    namespace CSConsole
    {
        class Class1
        {
            public static void Main()
            {
                WDGetContentControl(@"C:\Users\colbertz\Desktop\Document Author.docx", "");
            }

            public static OpenXmlElement WDGetContentControl(string fileName, string contentControlAlias)
            {
                OpenXmlElement oxe = null;

                using (var document = WordprocessingDocument.Open(fileName, true))
                {
                    var docPart = document.MainDocumentPart;
                    // Find the first content control whose Alias property
                    // matches the supplied name.
                    var sdts = docPart.Document.Descendants<SdtBlock>();
                    foreach (var sdt in sdts)
                    {
                        Console.WriteLine(sdt.SdtContentBlock.InnerText);
                    }
                }
                return oxe;
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, April 19, 2010 4:11 AM
    Moderator
  • Thanks Ji!
    • Marked as answer by pKulandrei Tuesday, April 20, 2010 10:57 PM
    Tuesday, April 20, 2010 10:56 PM

All replies

  • Hello,

    Yes. I think that is the correct path.

    I use the Open XML SDK 2.0 Productivity Tool to observe a test document which contains a content control. The following are xml file I got,

    -------------------------------------------

    <w:sdt xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
      <w:sdtPr>
        <w:alias w:val="Author" />
        <w:tag w:val="" />
        <w:id w:val="-737093943" />
        <w:placeholder>
          <w:docPart w:val="33042D41C0084166AB2D6D778BB64342" />
        </w:placeholder>
        <w:dataBinding w:prefixMappings="xmlns:ns0='http://purl.org/dc/elements/1.1/' xmlns:ns1='http://schemas.openxmlformats.org/package/2006/metadata/core-properties' " w:xpath="/ns1:coreProperties[1]/ns0:creator[1]" w:storeItemID="{6C3C8BC8-F283-45AE-878A-BAB7291924A1}" />
        <w:text />
      </w:sdtPr>
      <w:sdtContent>
        <w:p w:rsidR="00475400" w:rsidRDefault="00361B03">
          <w:r>
            <w:t>Document Author</w:t>
          </w:r>
        </w:p>
      </w:sdtContent>
    </w:sdt>

    -------------------------------------------

    From this, we can see if we want to retrieve the content value, we can get it from the path, w:sdtContent\w:p\w:r\w:t.

    Hope this helps!

     

    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, April 19, 2010 3:57 AM
    Moderator
  • By the way, if you are using OpenXML SDK 2.0, you can use the following codes to retrieve it,

    namespace CSConsole
    {
        class Class1
        {
            public static void Main()
            {
                WDGetContentControl(@"C:\Users\colbertz\Desktop\Document Author.docx", "");
            }

            public static OpenXmlElement WDGetContentControl(string fileName, string contentControlAlias)
            {
                OpenXmlElement oxe = null;

                using (var document = WordprocessingDocument.Open(fileName, true))
                {
                    var docPart = document.MainDocumentPart;
                    // Find the first content control whose Alias property
                    // matches the supplied name.
                    var sdts = docPart.Document.Descendants<SdtBlock>();
                    foreach (var sdt in sdts)
                    {
                        Console.WriteLine(sdt.SdtContentBlock.InnerText);
                    }
                }
                return oxe;
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, April 19, 2010 4:11 AM
    Moderator
  • Thanks Ji!
    • Marked as answer by pKulandrei Tuesday, April 20, 2010 10:57 PM
    Tuesday, April 20, 2010 10:56 PM
  • I have the same question but after get the content of the content control, I want to save it in another docx file and preserve style fonts etc...

    Do you have an idea about how I can do this?

    Thank you for your help.

    Tuesday, May 25, 2010 12:30 PM
  • Hi AmineBO,

    Thanks for your question.

    I think your quetion could be solved by "how to insert a content control into a Word document", right? If so, I suggest using the Productivity Tool to see the sample code by the following steps:

    1.      Create an empty docx document and save it, such as “source.docx”.
    2.      Add a content control together with the content value to the empty docx and save it as “target.docx”.
    3.      Open the Tool; click "Compare Files" to compare the “source.docx” and “target.docx” files.
    4.      In the "File Comparison" tab, click "View Part Diff" to show the differences between the selected two parts.
    5.       Click “View Package code” to see the complete sample code on how to add a content control.

    Hope this helps. If you have any question, please let me know.

    Thanks,

    Lu

    Friday, May 28, 2010 5:28 AM