From the sample xml, I am trying to select a book and then select the
author of the select book.
In the example code first SelectSingleNod e selects a book. Second
SelectSingleNod e on the book node should return the author of the
selected book (???). But I am getting the first author in the document
(Margaret Atwood), not the author of the selected book (Jane Austen).
Thanks.
Sample Code:
-----------------------------------------------
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlDocument doc = new XmlDocument();
doc.Load("books ort.xml");
//Create an XmlNamespaceMan ager for resolving namespaces.
XmlNamespaceMan ager nsmgr = new
XmlNamespaceMan ager(doc.NameTa ble);
nsmgr.AddNamesp ace("bk", "urn:sample s");
//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentEle ment;
book =
root.SelectSing leNode("/descendant::boo k[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLi ne(book.OuterXm l);
author = book.SelectSing leNode("/descendant::aut hor");
Console.WriteLi ne(author.Outer Xml);
}
-----------------------------------------------
Sample XML:
-----------------------------------------------
<?xml version="1.0" ?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:s amples">
<book genre="novel" publicationdate ="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
<book genre="novel" publicationdate ="1997" bk:ISBN="1-861001-57-8">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate ="1991" bk:ISBN="1-861001-57-6">
<title>Emma</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" publicationdate ="1982" bk:ISBN="1-861001-45-3">
<title>Sense and Sensibility</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>19.95</price>
</book>
</bookstore>