0

次のサンプル XML を books.xml に保存しました。

<?xml version="1.0" encoding="utf-8" ?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
    <description>
      An in-depth look at creating applications
      with XML.
    </description>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
    <description>
      A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.
    </description>
  </book>
</catalog>

次のようなドキュメントとナビゲーターを作成しました。

var document = new XPathDocument(@"books.xml");
var navigator = document.CreateNavigator();
var books = navigator.Select("/catalog/book");

本のノードを調べて、ノードのコンテキストを解析しようとしています。属性を読み取ることはできますが、ノードの値を読み取る方法がわかりません。

while (books.MoveNext())
{
    var location = books.Current;
    var book = new Book();
    book.Id = location.GetAttribute("id", "");
                    
    // this line throws an exception.
    book.Title = (string)location.Evaluate("title/text()") ;
}

私がドキュメントから逃したものについての洞察を持っている人はいますか?

XElement、XmlDocument、および XmlTextReader の解析メソッドについては承知していますが、パフォーマンス比較のために XPathNavigator がどのように機能するかを理解する必要があります。

ティア。

4

2 に答える 2

0

XmlReader を試しましたか?

var reader = new XmlReader("");
while(reader.ReadToFolowing("book")){
    reader.ReadInnerXml();
}
于 2013-05-26T00:26:31.080 に答える