Adding a new node to a relatively specific spot in an XML file

OmaJohn

New member
Joined
Nov 28, 2011
Messages
2
Programming Experience
5-10
Howdy, guys.

I've been perusing this section a little here and there the last couple of days, and Google extensively, without finding a good example for what I need. I've decided I'd better actually ask. I believe the contrast between the solutions I see and my code is a minor misalignment of my perspective (xpath or something similar is scratching at the front door of my brain).

I'm modifying an XML document with a structure along these lines:

VB.NET:
< config>
  <something par="true">
    <other stuff="false" />
  </something>
  <!-- Below are important things-->
  <impstuff>
    <whynot paramstuff="whatever" />
    <lilgroop>
      <lilsetting lilpar="true" />
    </lilgroop>
    <fordrules rwd="true" />
  </impstuff>
< /config>

I want to add a new node, "myNode" with a parameter "myParameter" value "true", but I want it to fall in line here:

VB.NET:
< config>
  <something par="true">
    <other stuff="false" />
  </something>
  <!-- Below are important things-->
  <impstuff>
    <whynot paramstuff="whatever" />
    <[B]myNode myParameter="true" />[/B]
    <lilgroop>
      <lilsetting lilpar="true" />
    </lilgroop>
    <fordrules rwd="true" />
  </impstuff>
< /config>


It seems like there should be an easy way to do it, but it's not jumping out at me. The following code works, but I don't control the XML document, so relying on indexes seems bad -- and I'm positive it's way more code than I need, plusalso blatant misuse of the concept of subroutines.

VB.NET:
    Public Sub AddSetting(ByRef objXMLDoc As Xml.XmlDocument)
        Try
            ' Create a new element node.
            Dim objNode As XmlNode
            Dim objAttribute As XmlAttribute
            objAttribute = objXMLDoc.CreateAttribute(XmlNodeType.Attribute, "myParameter", "")
            objAttribute.Value = "true"
            objNode = objXMLDoc.CreateNode(XmlNodeType.Element, "myNode", "")
            objNode.Attributes.Append(objAttribute)
            objXMLDoc.ChildNodes.Item(0).ChildNodes.Item(2).AppendChild(objNode)
            LogWrite("Display the modified XML document...")
            LogWrite(objXMLDoc.OuterXml)
        Catch ex As Exception
            Debug.Print("AddSetting() Exception: " & _
                "   Exception.Message: " & ex.Message & vbCrLf & _
                "   Exception.HelpLink: " & ex.HelpLink & vbCrLf & _
                "   Exception.Source: " & ex.Source & vbCrLf & _
                "   Exception.StackTrace: " & ex.StackTrace & vbCrLf & _
                "   Exception.TargetSite: " & ex.TargetSite.ToString)
        End Try
    End Sub

I'm leaving this rather vague because I'm hoping someone comfortable with the System.Xml object model can say, "oh, easy, just do these couple of lines of code," rather than debugging some minor syntax error or slightly improving my logic.

So, any thoughts? :)
 
SelectSingleNode to get 'whynot' node, then use its parent node InsertAfter method.
Dim sibling = objXMLDoc.SelectSingleNode("config/impstuff/whynot")
sibling.ParentNode.InsertAfter(objNode, sibling)

Also:
- CreateAttribute method has no compatible signature with your code, specifying a node type is something you only do with CreateNode method. Likewise your CreateNode call for node type Element can be simplified with using CreateElement method.
- Remove the ByRef parameter passing instruction.
- XPath Tutorial
 
Thanks, JohnH. Fantastic. Gets me right where I need to be now, and points me in the right direction for the next stuff I want to do. Thank you kindly.
 
Back
Top