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开发者_如何学C 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?
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?
精彩评论