Here's my first foray into linq to XML. The task here is to take an XMLDocument and see if a particular node exists. If not, we need to add the node.
Sub handleSopDocs(ByRef xDoc As XmlDocument)
Dim eConnect As XElement
'load the document using the 'string' method
eConnect = XElement.Parse(xDoc.OuterXml)
'loop through all the 'SOPTransactionType' nodes in the document
For Each SOPTransactionType In eConnect.Elements("SOPTransactionType")
'get a reference to that documnet's 'taSopHdrIvcInsert' node
Dim taSopHdrIvcInsert As XElement = SOPTransactionType.Element("taSopHdrIvcInsert")
'get the sopnumbe node. this will always exist.
Dim SOPNUMBE As XElement = taSopHdrIvcInsert.Element("SOPNUMBE")
'get the string value of the sopnumbe
Dim strSOPNUMBE As string = taSopHdrIvcInsert.Element("SOPNUMBE")
'get the BACHNUMB node. this may or may not exist
Dim bachnumb As XElement = taSopHdrIvcInsert.Element("BACHNUMB")
'if it did not exist...
If bachnumb Is Nothing Then
'create a new node
bachnumb = New XElement("BACHNUMB", "ORDERS")
'add in after the SOPNUMBE
SOPNUMBE.AddAfterSelf(bachnumb)
End If
Next
'this procedure is 'BY REF', so we reload the xDoc parameter and we're good to go
xDoc.LoadXml(eConnect.ToString)
End Sub