Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By JohnH

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

  1. #1
    OmaJohn is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    2
    Reputation
    0

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

    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:

    Code:
    < 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:

    Code:
    < config>
      <something par="true">
        <other stuff="false" />
      </something>
      <!-- Below are important things-->
      <impstuff>
        <whynot paramstuff="whatever" />
        <myNode myParameter="true" />
        <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.

    Code:
        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?

  2. #2
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,626
    Reputation
    2739
    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
    OmaJohn likes this.

  3. #3
    OmaJohn is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    2
    Reputation
    0
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking