By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,556 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Xpath On selected node??

P: n/a
Hi,
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 SelectSingleNode selects a book. Second
SelectSingleNode 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("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");

Console.WriteLine(author.OuterXml);
}
-----------------------------------------------
Sample XML:
-----------------------------------------------
<?xml version="1.0" ?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<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>

Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
probashi,

I believe you want to use the expression "descendant::author". I
believe using the slash bumps the expression back to the root of the
document.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"probashi" <pr******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,
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 SelectSingleNode selects a book. Second
SelectSingleNode 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("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");

Console.WriteLine(author.OuterXml);
}
-----------------------------------------------
Sample XML:
-----------------------------------------------
<?xml version="1.0" ?>
<!-- a fragment of a book store inventory database -->
<bookstore xmlns:bk="urn:samples">
<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>

Nov 16 '05 #2

P: n/a
"probashi" <pr******@hotmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi,
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 SelectSingleNode selects a book. Second
SelectSingleNode 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("booksort.xml");

//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:samples");

//Select the book node with the matching attribute value.
XmlNode book, author;
XmlElement root = doc.DocumentElement;
book =
root.SelectSingleNode("/descendant::book[@bk:ISBN='1-861001-57-6']",
nsmgr);
Console.WriteLine(book.OuterXml);

author = book.SelectSingleNode("/descendant::author");


[...]

I can't test it right now, but I guess that should read:

author = book.SelectSingleNode("./descendant::author");

The dot is saying, select from the current branch. If you don't add it, it
will select from the root of the document.

Let me know if this works.

By the way, I think you can select the author in one statement. Check
following links for more information:

http://www.w3schools.com/xpath/
http://www.w3.org/TR/xpath

Cheers,
---
Tom Tempelaere

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.