这是一个简化的mxl结构'xml',
<store>
<book_1>
<author_1><name>Alice</name><age>30</age></author_1>
<author_2><name>Bob</name><age>31</age></author_2>
<book_1>
<author_1><name>Charley</name><age>29</age></author_1>
<author_2><name>Dory</name><age>25</age></author_2>
<book_1>
</store>
这是我尝试过的;
XmlDocument submission = new XmlDocument();
submission.LoadXml(xml);
var bookNodes = submission.SelectNodes("//*[starts-with(local-name(),'book_')]");
这给了我一本书的清单。
foreach (XmlNode book in bookNodes)
{
//I want to do something like to find book authors for the book in context e.g. for the first book I just want nodes for Alice and Bob.
// var bookAuthors = book.SelectNodes("decendants::[starts-with(local-name(),'author_')");
}
我怎样才能开始检查后代元素?
编辑: 好像这是一个错字......
var bookAuthors = book.SelectNodes(“ descendant :: * [starts-with(local-name(),'MeritCriterion _')] ”);
答案 0 :(得分:1)
您可以使用以下XPath语法访问后代节点:
XmlDocument submission = new XmlDocument();
submission.LoadXml(xml);
var bookNodes = submission.SelectNodes("//*[starts-with(local-name(),'book_')]");
foreach (XmlNode book in bookNodes)
{
var author = book.SelectNodes("descendant::*[starts-with(local-name(),'author_')]");
foreach (XmlNode authorInfo in author)
{
Console.WriteLine(authorInfo .InnerText);
}
}
简而言之,您需要访问descendant::(all)[starts-with]
,否则您只是尝试在XPath中不访问任何后代。 :)