<bookstore>
<book>
<title>bob</title>
<author>fred</author>
</book>
...
使用C#XmlTextReader
,如何在图书标题为bob
时打印出作者?
答案 0 :(得分:0)
您可以使用XmlDocument
希望你明白需要做些什么。
答案 1 :(得分:0)
您可以使用xpath查找节点:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("yourfile.xml");
string path = "/bookstore/book[title='bob']"; // find the book node only when the book title is bob
XmlNode node = xmlDoc.SelectSingleNode(path); // get the book node
string author = node.SelectSingleNode("author").InnerText; // find the author node, return its inner text
如果书名的值不唯一,则可以使用XmlDocument.SelectNodes。
XmlNodeList books = xmlDoc.SelectNodes(path); // find all books whose title is bob
foreadh(XmlNode book in books)
{
string author = node.SelectSingleNode("author").InnerText;
...
}