# Adding a new Node in XML File

**URL:** <https://forums.thehomestack.io/t/adding-a-new-node-in-xml-file/338>\
**Category:** vb.Net\
**Created:** [January 12, 2009, 1:39am UTC](https://forums.thehomestack.io/t/adding-a-new-node-in-xml-file/338 "2009-01-12T01:39:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [January 12, 2009, 1:39am UTC](https://forums.thehomestack.io/t/adding-a-new-node-in-xml-file/338/1 "2009-01-12T01:39:21Z")

</div>

here is a fimction to add a new node:

```auto

 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:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://forums.thehomestack.io/uploads/default/original/1X/161426793bc0d8bdf0d1a6692b3e44446861cc72.jpeg) [@system](https://forums.thehomestack.io/u/system)\
**Post date:** [January 12, 2009, 1:40am UTC](https://forums.thehomestack.io/t/adding-a-new-node-in-xml-file/338/2 "2009-01-12T01:40:59Z")

</div>

to remove a node use:

```auto
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

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

```
