Home

Maintenance of XML Elements

 

Locating an Element

 

Introduction

In some cases, you may want to perform an operation on an existing and particular node. For example, you may want to change the value of a node, you may want to add a new child node to an existing node, etc. Before taking any of these actions, you must be able to locate or identify the desired element.

Locating an element consists of looking for a particular node among the nodes. To do this, you must start somewhere. Obviously, the first node you can identify is the root. Once you get the root, you can then get a collection of its children. After getting a collection of the child nodes of the root, you can locate a node in the collection. If the node you are looking for is a child of that first collection, you can then get a collection of the child nodes of that node and proceed.

Fortunately, the System.Xml namespace provides various means of looking for a node in an XML document.

Locating an Element Using its Index

Consider the following XML file named videos.xml:

<?xml version="1.0" encoding="utf-8"?>
<videos>
    <video>
	<title>The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
    <video>
	<title>Her Alibi</title>
	<director>Bruce Beresford</director>
	<length>94 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>The Day After Tomorrow</title>
	<director>Roland Emmerich</director>
	<length>124 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>Other People's Money</title>
	<director>Alan Brunstein</director>
	<length>114 Minutes</length>
	<format>VHS</format>
	<rating>PG-13</rating>
    </video>
</videos>

In Lesson 8, we saw that the XmlNodeList class was equipped with both a method named Item and an indexed property (named Item). Their syntaxes are:

Public MustOverride Function Item(index As Integer) As XmlNode
Property Item(index As Integer) As XmlNode

These two members allow you to access an element based on its index. Here are examples:

Imports System.Xml
Imports System.IO

Public Class Exercise
    Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            Dim RootElement As XmlElement = DOMDocument.DocumentElement
            Dim ListOfVideos As XmlNodeList = RootElement.ChildNodes

            MsgBox(ListOfVideos(1).InnerText)
            MsgBox(ListOfVideos.Item(3).InnerXml)
        End If
    End Sub
End Class

This would produce:

Videos

Videos

You can use this characteristic to locate a node. Because XML is very flexible with the names (you can have two child nodes that have the same name) and values (you can have two child nodes that have the same value) of nodes, when creating an XML document, it is your responsibility to create a scheme that would eventually allow you to uniquely identify each

Locating an Element Using its Name

To assist you with locating the first child node of a node, the XmlNode class is equipped with an indexed property (named Item) overloaded with two versions. One of the versions is declared as follows:

Public Overridable ReadOnly Property Item(name As String) As XmlElement

This indexed property takes the name of a node as argument. After the property has been called, the parser checks the child nodes of the element on which the property was applied. If the parser finds a node with that name, it returns it as an XmlElement object. Here is an example:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            Dim RootElement As XmlElement = DOMDocument.DocumentElement
            Dim ListOfVideos As XmlNodeList = RootElement.ChildNodes

            MsgBox(ListOfVideos(1)("director").InnerText)
            MsgBox(ListOfVideos.Item(3)("format").InnerXml)
        End If
End Sub

Based on the videos.xml file we had earlier, this would produce:

Videos Videos

If the node has more than one child with the same name, then it would return the first child with that name. You can use this characteristic to look for, or locate, an element.

Locating an Element Using a Tag Name

To assist you with finding a node, the XmlDocument class is equipped with the GetElementByTagName() method which is overloaded with two versions. One of the syntaxes used is:

Public Overridable Function GetElementsByTagName ( _
	name As String _
) As XmlNodeList

This method takes as argument a string. The string must be the name of a node. If at least one node that holds that name exists in the file, this method returns a collection of the nodes with that name. If there is no node with that name, the collection is returned empty and there is no exception thrown.

Here is an example of calling the method:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            ' Get a reference to the root node
            Dim RootElement As XmlElement = DOMDocument.DocumentElement

            ' Create a list of nodes whose name is Title
            Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")
        End If
End Sub

Once you have a list of the nodes of a particular criterion, you can then act as you see fit. For example, you can look for a particular node that holds text of your choice. Here is an example:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            ' Get a reference to the root node
            Dim RootElement As XmlElement = DOMDocument.DocumentElement

            ' Create a list of nodes whose name is Title
            Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")

            For Each Node As XmlNode In ListOfTitles
                If Node.InnerText = "Her Alibi" Then

                End If
            Next
        End If
End Sub

Inserting an Element

 

Inserting an Element as a Last Child

Once again, consider our Videos.xml file:

<?xml version="1.0" encoding="utf-8"?>
<videos>
    <video>
	<title>The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
    <video>
	<title>Her Alibi</title>
	<director>Bruce Beresford</director>
	<length>94 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>The Day After Tomorrow</title>
	<director>Roland Emmerich</director>
	<length>124 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>Other People's Money</title>
	<director>Alan Brunstein</director>
	<length>114 Minutes</length>
	<format>VHS</format>
	<rating>PG-13</rating>
    </video>
</videos>

Imagine you want to add a list of actors of the Her Alibi video. The first action to take is to locate the video, which you can do by calling the XmlDocument.GetElementsByTagName() method applied to a collection of nodes whose names are video. From this list of nodes, you can look for the node whose value is "Her Alibi". Once you have found this element, get a reference to its parent. Then add the new node as a child its parent. This can be done as follows:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            ' Get a reference to the root node
            Dim RootElement As XmlElement = DOMDocument.DocumentElement

            ' Create a list of nodes whose name is Title
            Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")

            ' Now you can check each node of the list
            For Each Node As XmlNode In ListOfTitles
                If Node.InnerText = "Her Alibi" Then

                    ' Create an element named Actors
                    Dim NewElement As XmlElement = DOMDocument.CreateElement("actors")
                    Dim Parent As XmlNode = Node.ParentNode
                    ' Add a new element named Actors to it
                    Parent.AppendChild(NewElement)
                    DOMDocument.Save(Filename)
                End If
            Next
        End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  . . .
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors />
  </video>
  . . .
</videos>

This code creates an empty element. If you want to create an element that includes a value, create its text and add that text to the node. Here is an example:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
    Dim Filename As String = "videos.xml"
    Dim DOMDocument As XmlDocument = New XmlDocument

    If File.Exists(Filename) Then
        DOMDocument.Load(Filename)

        ' Get a reference to the root node
        Dim RootElement As XmlElement = DOMDocument.DocumentElement

        ' Create a list of nodes whose name is Title
        Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")

        ' Now you can check each node of the list
        For Each Node As XmlNode In ListOfTitles
            ' When you get to a node, look for the element's value
            ' If you find an element whose value is Her Alibi
            If Node.InnerText = "The Distinguished Gentleman" Then
                ' Create an element named Category
                Dim NewElement As XmlElement = DOMDocument.CreateElement("category")
                ' Create the text of the new element
                Dim TextCatetory As XmlText = DOMDocument.CreateTextNode("Comedy")
                ' Get a reference to the parent of the node we have found
                Dim Parent As XmlNode = Node.ParentNode
                ' Add the new element to the node we found
                Parent.AppendChild(NewElement)
                ' Specify the text of the new node
                Parent.LastChild.AppendChild(TextCatetory)
                ' Save the file
                DOMDocument.Save(Filename)
            End If
        Next
    End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  . . .
</videos>

Using the same approach combined with what we learned about adding an item, you can add a new element that itself has child nodes. Here is an example:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
    Dim Filename As String = "videos.xml"
    Dim DOMDocument As XmlDocument = New XmlDocument

    If File.Exists(Filename) Then
        DOMDocument.Load(Filename)

        ' Get a reference to the root node
        Dim RootElement As XmlElement = DOMDocument.DocumentElement

        ' Create a list of nodes whose name is Title
        Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")

        ' Now you can check each node of the list
        For Each Node As XmlNode In ListOfTitles
            ' When you get to a node, look for the element's value
            ' If you find an element whose value is The Day After Tomorrow
            If Node.InnerText = "The Day After Tomorrow" Then
                ' Create an element named Actors
                Dim NewElement As XmlElement = DOMDocument.CreateElement("actors")
                ' Get a reference to the parent of the node we have found
                Dim VideoElement As XmlNode = Node.ParentNode
                ' Add the new element to the node we found
                VideoElement.AppendChild(NewElement)

                ' Create an element as a child of the new element
                ' Specify its name as Actor
                NewElement = DOMDocument.CreateElement("actor")
                ' Create the text of the new element
                Dim TextActor As XmlText = DOMDocument.CreateTextNode("Dennis Quaid")
                ' Add the new Actor element to the Actors node
                VideoElement.LastChild.AppendChild(NewElement)
                ' Specify the text of the new node
                VideoElement.LastChild.LastChild.AppendChild(TextActor)

                ' In the same way, add the other Actor nodes
                NewElement = DOMDocument.CreateElement("actor")
                TextActor = DOMDocument.CreateTextNode("Jake Gyllenhaal")
                VideoElement.LastChild.AppendChild(NewElement)
                VideoElement.LastChild.LastChild.AppendChild(TextActor)

                NewElement = DOMDocument.CreateElement("actor")
                TextActor = DOMDocument.CreateTextNode("Emmy Rossum")
                VideoElement.LastChild.AppendChild(NewElement)
                VideoElement.LastChild.LastChild.AppendChild(TextActor)

                NewElement = DOMDocument.CreateElement("actor")
                TextActor = DOMDocument.CreateTextNode("Dash Mihok")
                VideoElement.LastChild.AppendChild(NewElement)
                VideoElement.LastChild.LastChild.AppendChild(TextActor)

                ' Save the file
                DOMDocument.Save(Filename)
            End If
        Next
    End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  . . .
  <video>
    <title>The Day After Tomorrow</title>
    <director>Roland Emmerich</director>
    <length>124 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors>
      <actor>Dennis Quaid</actor>
      <actor>Jake Gyllenhaal</actor>
      <actor>Emmy Rossum</actor>
      <actor>Dash Mihok</actor>
    </actors>
  </video>
</videos>

You can also insert one or more elements as children of an existing node after locating that node. Here is an example:

XML File

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Mins</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors />
  </video>
</videos>

Code Fragment:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            ' Get a reference to the root node
            Dim RootElement As XmlElement = DOMDocument.DocumentElement

            ' Create a list of nodes whose name is Title
            Dim ListOfTitles As XmlNodeList = DOMDocument.GetElementsByTagName("title")

            ' Now you can check each node of the list
            For Each Node As XmlNode In ListOfTitles
                ' When you get to a node, look for the element's value
                ' If you find an element whose value is Her Alibi
                If Node.InnerText = "Her Alibi" Then
                    ' Get a reference to the video node that is 
                    ' the parent of the video titled Her Alibi
                    Dim VideoElement As XmlNode = Node.ParentNode

                    ' Create a list of the child nodes of the Her alibi video
                    Dim ListOfActors As XmlNodeList = VideoElement.ChildNodes

                    ' Visit each item of the collection
                    ' looking for an element named Actors
                    For Each ActorNode As XmlNode In ListOfActors
                        ' If you find an element named Actors
                        If ActorNode.Name = "actors" Then
                            ' Create a new element named Actor
                            ' Specify its name as Actor
                            Dim NewElement As XmlElement = DOMDocument.CreateElement("actor")
                            ' Create the text of the new element
                            Dim TextActor As XmlText = DOMDocument.CreateTextNode("Tom Selleck")
                            ' Add the new Actor element to the Actors node
                            VideoElement.LastChild.AppendChild(NewElement)
                            ' Specify the text of the new node
                            VideoElement.LastChild.LastChild.AppendChild(TextActor)

                            ' Add other Actor nodes
                            NewElement = DOMDocument.CreateElement("actor")
                            TextActor = DOMDocument.CreateTextNode("Paulina Porizkova")
                            VideoElement.LastChild.AppendChild(NewElement)
                            VideoElement.LastChild.LastChild.AppendChild(TextActor)

                            NewElement = DOMDocument.CreateElement("actor")
                            TextActor = DOMDocument.CreateTextNode("William Daniels")
                            VideoElement.LastChild.AppendChild(NewElement)
                            VideoElement.LastChild.LastChild.AppendChild(TextActor)

                            ' Save the file
                            DOMDocument.Save(Filename)

                            ' Stop, in this example, we don't expect another Actors node
                            Exit For
                        End If
                    Next
                End If
            Next
        End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Minutes</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors>
      <actor>Tom Selleck</actor>
      <actor>Paulina Porizkova</actor>
      <actor>William Daniels</actor>
    </actors>
  </video>
</videos>

Inserting an Element Referencing a Sibling

Instead of simply adding a new node at the end of child nodes, you can specify any other position you want. For example, you may want the new node to precede an existing child node. To support this operation, the XmlNode class provides the InsertBefore() method. Its syntax is:

Public Overridable Function InsertBefore ( _
	newChild As XmlNode, _
	refChild As XmlNode _
) As XmlNode

The first argument of this method is the new node that will be added. The second argument is the sibling that will succeed the new node. Consider the following version of our Videos.xml file:

<?xml version="1.0" encoding="utf-8"?>
<videos>
    <video>
        <title>The Distinguished Gentleman</title>
        <director>Jonathan Lynn</director>
        <length>112 Minutes</length>
        <format>DVD</format>
        <rating>R</rating>
        <category>Comedy</category>
    </video>
    <video>
	<title>Fatal Attraction</title>
	<director>Adrian Lyne</director>
	<length>119 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
</videos>

Imagine you want to create a new category element below the director element whose name is Adrian Lyne. You can first get a list of videos. Inside of each video, check the nodes and find out whether the video has a director node whose text is Adrian Lyne. Once you find that node, you can add the new element after it.

As we have pointed out in previous sections and lessons, using a value such as the name of a director of a video to locate a node is not a smart idea because you could have various videos with the same director. In professional applications, always make sure you have a way to uniquely distinguish each node in a category or collection. For a video collection such as ours, you could create a node that has a shelf number or the ISBN number that is unique to each video, and use that number to identify a particular video.

Here is an example:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
    Dim Filename As String = "videos.xml"
    Dim DOMDocument As XmlDocument = New XmlDocument

    If File.Exists(Filename) Then
        DOMDocument.Load(Filename)

        ' Get a reference to the root node
        Dim RootElement As XmlElement = DOMDocument.DocumentElement

        ' Create a list of the videos
        Dim ListOfVideos As XmlNodeList = DOMDocument.GetElementsByTagName("video")

        ' visit each video
        For Each Node As XmlNode In ListOfVideos
            ' Within a video, create a list of its children
            Dim ListOfChildren As XmlNodeList = Node.ChildNodes

            ' Visit each child node
            For Each Director As XmlNode In ListOfChildren
                ' If the child node is (a director and its name is) Adrian Lyne
                If Director.InnerText = "Adrian Lyne" Then
                    ' Create an element named Category
                    Dim NewElement As XmlElement = DOMDocument.CreateElement("category")
                    ' Specify the text of the new element
                    NewElement.InnerText = "Drama"

                    ' Insert the new node below the Adrian Lyne node Director
                    Node.InsertAfter(NewElement, Director)

                    ' Save the file
                    DOMDocument.Save(Filename)

                    ' Stop
                    Exit For
                End If
            Next
        Next
    End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  <video>
    <title>Fatal Attraction</title>
    <director>Adrian Lyne</director>
    <category>Drama</category>
    <length>119 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
</videos>

In the same way, you can insert a new node after a child of a child (or of a child of a child of a child) of any node. If you want to new node to be positioned after an existing child node, you can call the XmlNode.InsertAfter() method. Its syntax is:

Public Overridable Function InsertAfter ( _
	newChild As XmlNode, _
	refChild As XmlNode _
) As XmlNode

Deleting Elements

 

Deleting an Element

If you have a node you do not want or do not need it anymore in the document, you can delete it. To delete a node, the XmlNode class provides the RemoveChild() method. Its syntax is:

Public Overridable Function RemoveChild(oldChild As XmlNode) As XmlNode

This method takes as argument the node to delete. If the node exists, it would be deleted and the method would return the node that was deleted. If the node does not exist, nothing would happen. To effectively use this method, you should first locate the particular node you want to delete. You can look for it using any of the logics we have applied so far. Once you find the node, you can then delete it. Consider the following document:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  <video>
    <title>Her Alibi</title>
    <director>Bruce Beresford</director>
    <length>94 Mins</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors />
  </video>
</videos>

Imagine you want to delete a node whose name is Director and whose value is Bruce Beresford. Here is an example of calling this method to perform the operation:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
    Dim Filename As String = "videos.xml"
    Dim DOMDocument As XmlDocument = New XmlDocument

    If File.Exists(Filename) Then
        DOMDocument.Load(Filename)

        ' Get a reference to the root node
        Dim RootElement As XmlElement = DOMDocument.DocumentElement

        ' Create a list of the videos
        Dim ListOfVideos As XmlNodeList = DOMDocument.GetElementsByTagName("video")

        ' visit each video
        For Each Node As XmlNode In ListOfVideos
            ' Within a video, get a list of its children
            Dim ListOfChildren As XmlNodeList = Node.ChildNodes

            ' Visit each child node
            For Each Director As XmlNode In ListOfChildren
                ' If the child node is Bruce Beresford
                If Director.InnerText = "Bruce Beresford" Then
                    Node.RemoveChild(Director)

                    ' Save the file
                    DOMDocument.Save(Filename)

                    ' Stop
                    Exit For
                End If
            Next
        Next
    End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
    <category>Comedy</category>
  </video>
  <video>
    <title>Her Alibi</title>
    <length>94 Mins</length>
    <format>DVD</format>
    <rating>PG-13</rating>
    <actors />
  </video>
</videos>

Notice that the <director>Bruce Beresford</director> node is gone.

Clearing an Element of its Children

To delete all child nodes of a node, you can call the XmlNode.RemoveAll() method. Its syntax is:

Public Overridable Sub RemoveAll

When called, this method will remove all child nodes, if any, of their parent node. Consider the following XML file named videos.xml:

<?xml version="1.0" encoding="utf-8"?>
<videos>
    <video>
	<title>The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
    <video>
	<title>Her Alibi</title>
	<director>Bruce Beresford</director>
	<length>94 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>The Day After Tomorrow</title>
	<director>Roland Emmerich</director>
	<length>124 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
    </video>
    <video>
	<title>Other People's Money</title>
	<director>Alan Brunstein</director>
	<length>114 Minutes</length>
	<format>VHS</format>
	<rating>PG-13</rating>
    </video>
</videos>

Here is an example of calling the XmlNode.RemoveAll() method to delete all child nodes of any node named video in the document:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
        Dim Filename As String = "videos.xml"
        Dim DOMDocument As XmlDocument = New XmlDocument

        If File.Exists(Filename) Then
            DOMDocument.Load(Filename)

            ' Get a reference to the root node
            Dim RootElement As XmlElement = DOMDocument.DocumentElement

            ' Create a list of the videos
            Dim ListOfVideos As XmlNodeList = DOMDocument.GetElementsByTagName("video")

            ' visit each video
            For Each Node As XmlNode In ListOfVideos
                ' Within a video, get a list of its children
                Dim ListOfChildren As XmlNodeList = Node.ChildNodes

                ' Visit each child node
                For Each Director As XmlNode In ListOfChildren
                    Node.RemoveAll()

                    ' Save the file
                    DOMDocument.Save(Filename)
                Next
            Next
        End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video></video>
  <video></video>
  <video></video>
  <video></video>
</videos>

In the same way, you can first locate one particular parent node, and then delete all of its children by calling this method. Consider the following document:

<?xml version="1.0" encoding="utf-8"?>
<videos>
    <video>
	<title>The Distinguished Gentleman</title>
	<director>Jonathan Lynn</director>
	<length>112 Minutes</length>
	<format>DVD</format>
	<rating>R</rating>
    </video>
    <video>
	<title>The Day After Tomorrow</title>
	<director>Roland Emmerich</director>
	<length>124 Minutes</length>
	<format>DVD</format>
	<rating>PG-13</rating>
	<actors>
	    <actor>Dennis Quaid</actor>
	    <actor>Jake Gyllenhaal</actor>
	    <actor>Emmy Rossum</actor>
	    <actor>Dash Mihok</actor>
	</actors>
    </video>
    <video>
	<title>Other People's Money</title>
	<director>Alan Brunstein</director>
	<length>114 Minutes</length>
	<format>VHS</format>
	<rating>PG-13</rating>
    </video>
</videos>

Here is an example of deleting all child nodes of a video whose title is The Day After Tomorrow:

Private Sub btnDocument_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles btnDocument.Click
    Dim Filename As String = "videos.xml"
    Dim DOMDocument As XmlDocument = New XmlDocument

    If File.Exists(Filename) Then
        DOMDocument.Load(Filename)

        ' Get a reference to the root node
        Dim RootElement As XmlElement = DOMDocument.DocumentElement

        ' Create a list of the videos
        Dim ListOfVideos As XmlNodeList = DOMDocument.GetElementsByTagName("video")

        ' visit each video
        For Each Node As XmlNode In ListOfVideos
            ' Within a video, get a list of its children
            Dim ListOfChildren As XmlNodeList = Node.ChildNodes

            ' Visit each child node
            For Each TitleNode As XmlNode In ListOfChildren
                ' If the child node is Bruce Beresford
                If TitleNode.InnerText = "The Day After Tomorrow" Then
                    Node.RemoveAll()

                    ' Save the file
                    DOMDocument.Save(Filename)

                    ' Stop
                    Exit For
                End If
            Next
        Next
    End If
End Sub

This would produce:

<?xml version="1.0" encoding="utf-8"?>
<videos>
  <video>
    <title>The Distinguished Gentleman</title>
    <director>Jonathan Lynn</director>
    <length>112 Minutes</length>
    <format>DVD</format>
    <rating>R</rating>
  </video>
  <video>
  </video>
  <video>
    <title>Other People's Money</title>
    <director>Alan Brunstein</director>
    <length>114 Minutes</length>
    <format>VHS</format>
    <rating>PG-13</rating>
  </video>
</videos>

Previous Copyright © 2008-2012 FunctionX Next