2014-02-28 23 views
0

下文提到的是我的XML的一部分:如何使用XPathDocument根據ID搜索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> 

我想在評論部分中的所有節點的細節,但選擇哪個部分將通過節點(NewCommentID)來決定。 在上面的示例中,作爲'NewCommentID = UniqueID02'中的值,我想使用XPathDocument在Comment部分的CommentID = UniqueID02下獲取所有標籤及其值。

有人可以指導如何使用VB.net實現相同?

回答

0

你可以試試這個方法:

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

navigator = xpathDoc.CreateNavigator() 
Dim result = navigator.Select("//Comment[./CommentId=../NewCommentID]") 
+0

感謝您的答覆,它的工作.. 但是,我想問一個..如果我沒有什麼NewCommentID標籤的更多問題XML並且在除了XML之外的其他一些變量中具有該價值,是否有可能實現上述方案? – user3363495

+0

只需將XPath中的'../ NewCommentID'替換爲您的變量值no?如果這不是你的意思,考慮發佈新的問題,更詳細地闡述這個新問題。理想情況下,一個職位應該有一個具體的問題*(由於很多原因,f.e使其更易於管理,因此未來的訪問者不會因爲試圖找出解決此問題的方式而感到困惑)。 – har07

+1

非常感謝您的建議:) – user3363495