Click here to Skip to main content
15,908,115 members
Articles / Programming Languages / XML

Recursive procedures to check if a given node is the immediate parent or a child node of another node in an XML DOM tree

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
20 Nov 2006CPOL 22.7K   6   1
These recursive procedures check for the immediate parent node or child node of another node.

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.

  1. 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.
  2. VB
    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
  3. 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.
  4. VB
    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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
I have been working as Application Engineer for 2+ years in ASP.NET

Comments and Discussions

 
GeneralI cannot see any other than SPAN tags Pin
Nirosh20-Nov-06 23:18
professionalNirosh20-Nov-06 23:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.