1

I am trying to build a list of specific elements within a Word Document using the OpenXML SDL 2.0. I will open a template and scan the Word template for all “w:sdt” elements which I will use as a list of available document parts. I will display this list to the user so they can chose the desired parts/elements to build a new version/copy of the document from the template document.

So my list will need to grab the sequential element number (this is used by the DocumentBuilder classes) as well as the “w:alias” “val” or maybe the “w:tag” “val” which will be used to display the selection option to the user.

Once the user has reviewed the available template XML part/elements and made their selection I will use DocumentBuilder to add the identified parts by sequential number to a new document.

I have successfully used DocumentBuilder to explicitly identify document elements by sequential number to build a new document. This works beautifully.

I know I need to use recursion to iterate through the template document and add the qualified elements to a LIST. I’m just not savvy enough yet with C# or the OpenXML SDK to identify the most elegant way to recurse through the template document targeting the desired XML part/elements.

Can anyone point me to an applicable example for enumerating a list of parts/elements in a OpenXML document?

  • ...so I'm researching and I think I should be looking at a SAX-Like approach presented in Brian Jones & Zeyad's blog post "Parsing and Reading Large Excel Files with the Open XML SDK". This allows for using an OpenXMLReader to traverse the XML Parts or Elements. I'm not clear exactly how to do this yet but it seems the way to go. – Cory Mathewson Jan 01 '11 at 03:53

1 Answers1

1

Unless there are performance reasons against it could you not open the document using the OpenXmlSdk and perform LINQ queries to locate each of your SDT elements?

Something along the lines of:

using(WordProcessingDocument doc = WordProcessingDocument.Open(byte[], false);){

     IEnumerable<SdtContent> sdtElements = doc.MainDocumentPart.Body.Descendants<SdtContent>();
     foreach(SdtContent el in sdtElements){
        // Collect content tag name/alias
        // Collect your sequential element number?
     }
}

I have used this approach before and filtered out external element to collect a list of SdtContent elements. Though I am not sure what you mean by sequential element numbers?

damoe
  • 221
  • 1
  • 3
  • After researching further I've been able to use the Builder classes to grab elements such as Paragraphs and SDT Blocks. Using LINQ and the OpenXML SDK 2.0 I can collect these elements in IEnurmerable objects. – Cory Mathewson Jan 19 '11 at 18:19
  • @damoe - Damoe, might you be able to assist me with the following:http://stackoverflow.com/questions/15791732/openxml-sdk-having-borders-for-cell – Nate Pet Apr 03 '13 at 16:16