The location of the text "NEW SECTION TO BE ADDED HERE" in your example suggests that you want the new data to be added as the last sibling to the first <SectionDataUpdateable />
. So I used this sample XML:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Nothing" SectionTypeCode="TEST" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>99</SectionSequence>
</Key>
<IsDeleted>dontcare</IsDeleted>
</SectionDataUpdateable>
<!--
/*********************************************
NEW SECTION TO BE INSERTED BEFORE THIS COMMENT
*********************************************/
-->
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
Without you defining the xmlns parts in the question to make it easier to answer, I had to make something up for "xsi" and remove the "r:".
One thing I have noticed about manipulating XML is that you often need to act on the parent of a node.
So, get the nodes "/BookUpdateable[1]/BookMaster/Sections/SectionDataUpdateable" and insert your new data after the last one:
Option Infer On
Option Strict On
Imports System.Xml
Module Module1
Sub Main()
Dim src = "C:\temp\BookUpdateable.xml"
Dim dest = "C:\temp\BookUpdateable2.xml"
Dim xmlDoc As New XmlDocument
Dim settings = New XmlReaderSettings With {.NameTable = New NameTable()}
Dim nsm = New XmlNamespaceManager(settings.NameTable)
nsm.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
Dim context = New XmlParserContext(Nothing, nsm, "", XmlSpace.Default)
Dim reader = XmlReader.Create(src, settings, context)
xmlDoc.Load(reader)
Dim nameCodeSections = xmlDoc.SelectNodes("/BookUpdateable[1]/BookMaster/Sections/SectionDataUpdateable", nsm)
Dim newData As XElement = <SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
Dim xd = New XmlDocument()
xd.LoadXml(newData.ToString())
nameCodeSections(0).ParentNode.InsertAfter(xmlDoc.ImportNode(xd.FirstChild, True), nameCodeSections(nameCodeSections.Count - 1))
xmlDoc.Save(dest)
Console.WriteLine("Done.")
Console.ReadLine()
End Sub
End Module
The running of which results in:
<?xml version="1.0" encoding="utf-8"?>
<BookUpdateable Action="Renew">
<BookOption>Book</BookOption>
<BookMaster Action="None">
<Key>
<MasterReference>7678812382</MasterReference>
</Key>
<Assured Action="Update" xsi:type="OrgDataUpdateable" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Key>
<CaptionCode>BOOKINTRO</CaptionCode>
<PrevTransaction>0</PrevTransaction>
</Key>
<Addresses>
<IsDeleted>true</IsDeleted>
</Addresses>
</Assured>
<Units>
<IsDeleted>true</IsDeleted>
</Units>
<Sections>
<SectionDataUpdateable Action="Update" SectionTypeCode="NEW" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>10</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Nothing" SectionTypeCode="TEST" SubSectionTypeCode="ONE">
<Key>
<SectionSequence>99</SectionSequence>
</Key>
<IsDeleted>dontcare</IsDeleted>
</SectionDataUpdateable>
<SectionDataUpdateable Action="Update" SectionTypeCode="OLD" SubSectionTypeCode="TWO">
<Key>
<SectionSequence>05</SectionSequence>
</Key>
<IsDeleted>true</IsDeleted>
</SectionDataUpdateable>
<!--
/*********************************************
NEW SECTION TO BE INSERTED BEFORE THIS COMMENT
*********************************************/
-->
</Sections>
<BookDataExts>
<MasterStatusCode>BOOKEXTN</MasterStatusCode>
<EffectiveDate>2017-07-28</EffectiveDate>
</BookDataExts>
</BookMaster>
<ReturnFields>false</ReturnFields>
</BookUpdateable>
You should put in some sanity checks such as testing that nameCodeSections IsNot Nothing
before using nameCodeSections(0)
.
References: