none
ChildElement's of a Body. RRS feed

  • Question

  • I am looking for a good tutorial on the subject of dealing with the ChildElement's of a WordProcessingDocumet's Body item.

    Ex. Body currentBody = currentDocument.MainDocumentPart.Document.Body;

    For instance how do I read them? Is there an index that is the location of the currently selected element? If not how would I read the elements so I can create my own array to handle them? I have read the "How To.." section of the OpenXML SDK help file and this is not really covered. I am currently reading the class definition. It would help if I had some further explanation, code samples or other tutorials. Is there anything available that covers this type of a subject?

    Thanks.

    Tuesday, November 30, 2010 11:58 PM

Answers

  • Hi Sepoto,

    First correct me if I am wrong, as I read, your problem was Get/Read all Child element of Body element. if so,

     

    1. Get main child elements from Body element.

    2. Do recursively look child element's child elements.

     


    foreach (var item in wpDocument.MainDocumentPart.Document.Body.ToList())
    {
              DoRecursiveFindText(extractString, item);
    }

     

    Recursive method,

            private void DoRecursiveFindText(OpenXmlElement item)
            {


     
                     // DO: Add find element to a list or what ever work Here.

               if (item.HasChildren)
                {

                     // Loop again for child elements.               
                    foreach (OpenXmlElement elementItem in item.ChildElements)
                    {
                        DoRecursiveFindText(elementItem);
                    }
                }
                else
                {

                     // DO: Add find element to a list or what ever work.
                }
            }

     

    OR

    If you need to find specific type,

    List<OpenXmlElement> elementList = wpDocument.MainDocumentPart.Document.Body.Descendants<OpenXmlElement>().ToList();



    - Vins
    Thursday, December 23, 2010 10:28 AM
  • Hi Sepoto,

    There are multiple ways to iterate w:Body depending upon your requirements. The most general is that it's an XML and you do whatever you want using XML classes.

    On “how to iterate in Body using Open XML SDK”, It will be good to have some basic knowledge of how Word Open XML is formed. Like Body can contain paragraphs, paragraph can contain runs and run contains the text. You could get all the paragraph in body by using method described above by Vins007 : wpDocument.MainDocumentPart.Document.Body.Descendants<OpenXmlElement>().

    For documentation, the best place I know of is the "Open XML SDK 2.0 Productivity Tool for Microsoft Office" which comes with Open XML SDK 2.0 download. You can go to Open XML SDK documentation (in the productivity tools), then browse to DocumentFormat.OpenXml.Wordprocessing and then look for w:body. You will find all the child elements which body can contain and which you can iterate over. If you don’t understand the meaning of child element, double click on it and productivity tool will open a page describing that element and its purpose.

    Thanks, Shiv

    Thursday, December 30, 2010 11:05 PM
    Moderator

All replies

  • Hi Sepoto,

    First correct me if I am wrong, as I read, your problem was Get/Read all Child element of Body element. if so,

     

    1. Get main child elements from Body element.

    2. Do recursively look child element's child elements.

     


    foreach (var item in wpDocument.MainDocumentPart.Document.Body.ToList())
    {
              DoRecursiveFindText(extractString, item);
    }

     

    Recursive method,

            private void DoRecursiveFindText(OpenXmlElement item)
            {


     
                     // DO: Add find element to a list or what ever work Here.

               if (item.HasChildren)
                {

                     // Loop again for child elements.               
                    foreach (OpenXmlElement elementItem in item.ChildElements)
                    {
                        DoRecursiveFindText(elementItem);
                    }
                }
                else
                {

                     // DO: Add find element to a list or what ever work.
                }
            }

     

    OR

    If you need to find specific type,

    List<OpenXmlElement> elementList = wpDocument.MainDocumentPart.Document.Body.Descendants<OpenXmlElement>().ToList();



    - Vins
    Thursday, December 23, 2010 10:28 AM
  • Hi Sepoto,

    There are multiple ways to iterate w:Body depending upon your requirements. The most general is that it's an XML and you do whatever you want using XML classes.

    On “how to iterate in Body using Open XML SDK”, It will be good to have some basic knowledge of how Word Open XML is formed. Like Body can contain paragraphs, paragraph can contain runs and run contains the text. You could get all the paragraph in body by using method described above by Vins007 : wpDocument.MainDocumentPart.Document.Body.Descendants<OpenXmlElement>().

    For documentation, the best place I know of is the "Open XML SDK 2.0 Productivity Tool for Microsoft Office" which comes with Open XML SDK 2.0 download. You can go to Open XML SDK documentation (in the productivity tools), then browse to DocumentFormat.OpenXml.Wordprocessing and then look for w:body. You will find all the child elements which body can contain and which you can iterate over. If you don’t understand the meaning of child element, double click on it and productivity tool will open a page describing that element and its purpose.

    Thanks, Shiv

    Thursday, December 30, 2010 11:05 PM
    Moderator
  • Both are good enough answers I guess. I have had to put the project aside for the time being. I have a more pressing project to work on. Thanks so much for the replies.
    Friday, December 31, 2010 12:07 AM