Parsing an Open XML Document Using Styled Blocks
I am working with docx docs and I need to parse a document into sections based on headings in a "heading 1" style. So if I had a document like this (pseudocode markup):
<doc>
<title style>Doc Title</title style>
<heading1>First Section</heading1>
...
<heading2>Second Section</heading2>
...
<heading3>Third Section</heading3>
...
</doc>
I would like to break this down into a document with four sections, the first of which is the content that precedes the first section. I suppose this is probably pretty straightforward once you are familiar with Open XML, but I don't.
TIA.
a source to share
Wow ... not even looking at this question all day. Well, I figured it out and thought I would share the wealth. I can't share this code directly, but it's just three nested loops, one loop through paragraphs, then a paragraph is executed, then styles. XPath for each one:
.//w:p
./w:pPr
./w:pStyle
Once you find a run with a style you like, you go back up the level to get the first run, which will contain the styled text. From there, it's just Comp Sci 101. I think the real breakthrough was to not even try to mess with the Open Xml SDK (other than the IO Packaging stuff) and go straight to XML processing.
a source to share