<bookstore>
<book>
<title>bob</title>
<author>fred</author>
</book>
...
C# を使用してXmlTextReader
、本のタイトルが の場合にのみ著者を出力するにはどうすればよいbob
ですか?
XmlDocumentを使用できます
何をする必要があるかを理解していただければ幸いです。
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;
...
}