简体   繁体   English

如何使用XPathDocument基于ID搜索XML标签?

[英]How to search XML Tags based on a ID using XPathDocument?

Below mentioned is a part of my XML: 下面提到的是我的XML的一部分:

<ParentNode>
    <NewCommentID>UniqueID02</NewCommentID>
    <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 the which section to select will be decided by Node (NewCommentID). 我想获取Comment部分下所有节点的详细信息,但是要选择哪个部分将由Node(NewCommentID)决定。 In the above example as the value in 'NewCommentID = UniqueID02', I want to fetch all the tags and their values under Comment section where the CommentID = UniqueID02 using XPathDocument. 在上面的示例中,作为“ NewCommentID = UniqueID02”中的值,我想使用XPathDocument获取Comment部分下的所有标签及其值,其中CommentID = UniqueID02。

Can someone guide how to achieve the same using VB.net? 有人可以指导如何使用VB.net实现相同目标吗?

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("//Comment[./CommentId=../NewCommentID]")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM