Introduction
This article explains two simple procedures to check if a given node is a child node or the immediate parent node of another node. An XML DOM tree consists of a number of XML nodes. If it is required to check if a node in an XML DOM tree is a child node or the immediate parent node in the same tree, use the simple procedures shown below. These are recursive procedures.
checkNode
is of type XmlNode
and used to check whether the node is a child node of parentNode
of type XmlNode
. Make sure that both are in the same XmlDocument
. Otherwise, it always returns False
.
Public Function IsCheckNodeChildNodeOfParentNode(ByRef parentNode _
As XmlNode, ByRef checkNode As XmlNode) As Boolean
Try
Dim xNode As XmlNode
If checkNode Is parentNode Then
Return True
ElseIf parentNode.HasChildNodes Then
For Each xNode In parentNode.ChildNodes
If checkNode Is xNode Then
Return True
Else
If IsCheckNodeChildNodeOfParentNode(xNode, _
checkNode) Then
Return True
End If
End If
Next
Else
Return False
End If
Return False
Catch ex As Exception
End Try
End Function
checkNode
is of type XmlNode
and is used to check whether the node is the immediate parent node of parentNode
of type XmlNode
. Make sure that both are in the same XmlDocument
. Otherwise, it always returns False
.
Public Function IsCheckNodeImmediateParentNodeOfParentNode(ByRef childNode _
As XmlNode, ByRef checkNode As XmlNode) As Boolean
Try
Dim parentNode As XmlNode = childNode.ParentNode
If Not parentNode Is Nothing Then
If Not parentNode.Name.ToLower.Trim = "#document" Then
If checkNode Is parentNode Then
Return True
Else
Return IsCheckNodeImmediateParentNodeOfParentNode(parentNode, _
checkNode)
End If
Else
Return False
End If
End If
Return False
Catch ex As Exception
End Try
End Function