0
<bookstore>
    <book>
         <title>bob</title>
         <author>fred</author>
     </book>
...

C# を使用してXmlTextReader、本のタイトルが の場合にのみ著者を出力するにはどうすればよいbobですか?

4

2 に答える 2

0

XmlDocumentを使用できます

  1. 「本」に getElementByTagName を使用する
  2. 要素を反復処理し、条件を使用して、「BOB」を含む正しいタイトルを取得します
  3. その親書籍ノードから著者値を取得します。

何をする必要があるかを理解していただければ幸いです。

于 2013-03-07T04:00:36.220 に答える
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;
    ...
}
于 2013-03-11T15:22:39.660 に答える