Skip to main content
 首页 » 编程设计

c#之如何根据其父节点的属性值查询XML节点

2025年05月04日53Leo_wl

我有一个看起来像这样的XML:

<Library> 
<Author Name = "JRR Tolkien"> 
<Book Title = "Lord Of the Rings" /> 
<Book Title = "The Hobbit" /> 
</Author> 
<Author Name = "JK Rowling"> 
<Book Title = "Harry Potter and the Sorcerers Stone" /> 
<Book Title = "Harry Potter and the Prisoner of Azkaban" /> 
</Author> 
</Library> 


XPath是什么,用于选择其父“ Author”节点的值为“ JK Rowling”的所有“ Book”节点?

请您参考如下方法:

这是最直观,最严格的选择,它将所有具有AuthorName属性为'JK Rowling'的书)作为父书的书。并且Author还必须具有Library作为父级。

/Library/Author[@Name='JK Rowling']/Book