0

I'm written a program that builds an XML structure (conforming to a specific schema) from a Word document. All is good apart from a small bug which is driving me mad.

If the Word doc has a 'para' directly after a sequencial list (numbered list), the para element needs to be positioned directly after the last closing tag in the structure. I do this a hundred times elsewhere but can't understand why this isn't having the desired effect.

Here is the structure before putting the new <para>some text here</para> into the XML (not where i've marked where I NEED the new element to be added):

<?xml version="1.0"?>
<descript>
    <para0>
        <title>Change blah blah blah</title>
        <para>This dialogue enables the user to change the things in the thing.</para>
        <para>To modify the number and associated thing:</para>
            <note>
                <para>Only available if the selected thing is in the thing.</para>
            </note>
        <para>
        <seqlist>
            <item>Click the Edit button.</item>
            <item>Click in the field.</item>
            <item>Enter the number then the format will be nnn.
            <note>
                <para>numbers cannot exceed the highest number blah blah.</para>
            </note>
            </item>
            <item>Click in the field.</item>
            <item>Enter the number.</item>
            </seqlist>
        </para> **<--this is where i need the new element to be added**
    </para0>
</descript>

This is the code i use to add the new <para>

content.Descendants("para")
.LastOrDefault()
.AddAfterSelf(new XElement(para, "some text here"));

This is what i get (note where it ACTUALLY gets added):

<?xml version="1.0"?>
<descript>
    <para0>
        <title>Change blah blah blah</title>
        <para>This dialogue enables the user to change the things in the thing.</para>
        <para>To modify the number and associated thing:</para>
            <note>
                <para>Only available if the selected thing is in the thing.</para>
            </note>
        <para>
        <seqlist>
            <item>Click the Edit button.</item>
            <item>Click in the Number field.</item>
            <item>Enter the number then the format will be nnn.
            <note>
                <para>numbers cannot exceed the highest number blah blah.</para>
                <para>some text here</para> **<--This is where it goes** 
            </note>
            </item>
            <item>Click in the field.</item>
            <item>Enter the number.</item>
            </seqlist>
        </para>
    </para0>
</descript>

I think i can see why it's happening (the para it gets added after is the last child <para> element), but it's not where I need it - I need it after the very last closing </para> tag of the document. Any ideas please?

Daedalus
  • 539
  • 2
  • 6
  • 16
  • 1
    Your explanation is correct - `Descendants` looks at every element, starting at the root and working inwards. That nested `para` is the last one. If the structure is always this, could you do something like `content.Descendants("para0").Elements("para").Last()`? – Charles Mager May 06 '21 at 08:40
  • This did the trick. Thanks so much. – Daedalus May 06 '21 at 09:50

0 Answers0