应该是下面这行所获得的值为null,所以无法获得其属性。
book=root.SelectSingleNode("descendant::book[price='24.95']");

解决方案 »

  1.   

    to 楼上大侠
    我知道是没有查询结果.
    可是不应该啊
    xml中有相应的数据节点啊xpath是按照msdn中照搬的
    不应该有错啊郁闷
      

  2.   

    try XmlDocument doc = new XmlDocument();
    doc.Load("..........");
             XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
    xnm.AddNamespace("xx","http://tempuri.org/booksort.xsd"); XmlNode book;
    XmlNode root = doc.DocumentElement; book=root.SelectSingleNode("xx:book[xx:price='24.95']", xnm);
    if (book != null)
    {
    //Change the price on the book.
    book.LastChild.InnerText="15.95";
    }
      

  3.   

    your xml has a default namespace:
    <bookstore xmlns="http://tempuri.org/booksort.xsd">
              ^^^^^^so all your elements are under "http://tempuri.org/booksort.xsd", you could remove it
    <bookstore xmlns:bk="urn:samples">then your old code will work