Question

Below mentioned is a part of my XML:

sVariableValue = "UniqueID02"


    <ParentNode>
            <Comment>
            <CommentId>UniqueID01</CommentId> 
            <CommentDesc>Some comments</CommentDesc> 
            <CommentTypeCd>Code1</CommentTypeCd> 
            <CreatedDt>2013-11-29</CreatedDt> 
            <CreatedByUserId>user01</CreatedByUserId> 
            <GivenName>Mitchell</GivenName> 
            <Surname>Johnson</Surname> 
        </Comment>
        <Comment>
            <CommentId>UniqueID02</CommentId> 
            <CommentDesc>Some Comments....</CommentDesc> 
            <CommentTypeCd>Code2</CommentTypeCd> 
            <CreatedDt>2013-11-29</CreatedDt> 
            <CreatedByUserId>user02</CreatedByUserId> 
            <GivenName>Mike</GivenName> 
            <Surname>Jobs</Surname> 
        </Comment>
    </ParentNode>

I want to get the details of all the nodes under Comment section, but which section to select will be decided by value of (sVariableValue). In the above example as the value in 'sVariableValue = UniqueID02', I want to fetch all the tags and their values under Comment section where the CommentID = UniqueID02 using XPathDocument.

Can someone guide how to achieve the same using VB.net?

Was it helpful?

Solution

Here is one way.

Private Sub ReadComment()
    Dim oDoc As XPathDocument
    Dim oNav As XPathNavigator
    Dim oComment As XPathNavigator
    Dim oChild As XPathNavigator
    Dim sVariableValue = "UniqueID02"

    Dim oElem As XElement = <ParentNode>
                                <Comment>
                                    <CommentId>UniqueID01</CommentId>
                                    <CommentDesc>Some comments</CommentDesc>
                                    <CommentTypeCd>Code1</CommentTypeCd>
                                    <CreatedDt>2013-11-29</CreatedDt>
                                    <CreatedByUserId>user01</CreatedByUserId>
                                    <GivenName>Mitchell</GivenName>
                                    <Surname>Johnson</Surname>
                                </Comment>
                                <Comment>
                                    <CommentId>UniqueID02</CommentId>
                                    <CommentDesc>Some Comments....</CommentDesc>
                                    <CommentTypeCd>Code2</CommentTypeCd>
                                    <CreatedDt>2013-11-29</CreatedDt>
                                    <CreatedByUserId>user02</CreatedByUserId>
                                    <GivenName>Mike</GivenName>
                                    <Surname>Jobs</Surname>
                                </Comment>
                            </ParentNode>



    Using oSr As New StringReader(oElem.ToString)
        oDoc = New XPathDocument(oSr)
        oNav = oDoc.CreateNavigator
        oComment = oNav.SelectSingleNode("child::ParentNode/Comment[CommentId='" & sVariableValue & "']")
        If Not oComment Is Nothing Then
            oChild = oComment.SelectSingleNode("child::CommentId")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::CommentDesc")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::CommentTypeCd")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::CreatedDt")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::CreatedByUserId")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::GivenName")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If
            oChild = oComment.SelectSingleNode("child::Surname")
            If Not oChild Is Nothing Then
                Debug.Print(oChild.Value)
            End If

        End If

    End Using
End Sub

OTHER TIPS

You can try this way :

Dim xpathDoc As New XPathDocument("Path_to_xml_file.xml")
Dim navigator As XPathNavigator

navigator = xpathDoc.CreateNavigator()
Dim result = navigator.Select(String.Format("//Comment[./CommentId='{0}'", sVariableValue))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top