Adding a new Node in XML File

here is a fimction to add a new node:


 Private Sub AddNode(ByVal filename As String, ByVal name As String, ByVal ip As String, ByVal group As String, ByVal desc As String)
        Dim doc As New XmlDocument
        Dim root As XmlNode
        doc.Load(filename)
        root = doc.DocumentElement

        Dim newNode As XmlNode = doc.CreateElement("Node")
        Dim newChildNode As XmlNode = doc.CreateElement("Name")
        newChildNode.InnerText = name
        newNode.AppendChild(newChildNode)
        newChildNode = doc.CreateElement("IPAddress")
        newChildNode.InnerText = ip
        newNode.AppendChild(newChildNode)

        newChildNode = doc.CreateElement("GroupFile")
        newChildNode.InnerText = group
        newNode.AppendChild(newChildNode)

        newChildNode = doc.CreateElement("Description")
        newChildNode.InnerText = desc
        newNode.AppendChild(newChildNode)
        root.AppendChild(newNode)

        doc.Save(filename)
    End Sub

You can call this function with the following, where the first argument is a path to your xml file:

AddNode(filename, "ABC", "111.111.111.111", ".\file.xml", "ABC New Node")

to remove a node use:

Private Sub DeleteNode(ByVal filename As String, ByVal name As String)
        Dim doc As New XmlDocument
        Dim root As XmlNode
        doc.Load(filename)
        root = doc.DocumentElement
        Dim node As XmlNode = doc.SelectSingleNode("//Node/Name[text()='" & name & "']")
        root.RemoveChild(node.ParentNode)

        doc.Save(filename)
    End Sub

call it as

DeleteNode('file path here", "node name here")