473,657 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Xpath On selected node??

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 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>

Nov 16 '05 #1
2 3479
probashi,

I believe you want to use the expression "descendant::au thor". 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.co m

"probashi" <pr******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.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 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>

Nov 16 '05 #2
"probashi" <pr******@hotma il.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.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 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");


[...]

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

author = book.SelectSing leNode("./descendant::aut hor");

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6817
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for elements, attributes, ".", and "..", plus also the "" predicate format is supported - however, only one predicate per path step is supported, and expr must be a relative path. 2. Poor performance
1
5461
by: kurt hansen | last post by:
hi I thought that this would be easy, but maybe not so much. I want to: pass an xpath expression and a string value to a stylesheet and copy the source xml document, changing the value of the node described by the xpath expression, to the new value.
4
5836
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
3
1524
by: Patrick | last post by:
Dear Ng, after I have loaded am XmlDataDocument file, I try to extract a single node via XmlElement myElem = (XmlElement)this.XmlDataDocument.SelectSingleNode("/Document/Headerdata/myElem"); this.myProperty = myElem.InnerText; But the myElem object stays undefined. The XPath is definitly correct, if I
13
3238
by: David Thielen | last post by:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software" nav.SelectSingleNode"."); The select returns an exception: + $exception {"'.' has an invalid token."} System.Exception {System.Xml.XPath.XPathException} Any idea why? --
9
2146
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they should work. Also the second nav.OuterXml appears to also be wrong to me. Can someone explain to me why this does not work? (This is an example from a program we have where xpath can be entered in two parts so we have to be able
1
1781
by: woodworthjames | last post by:
Hello, not sure i am using xpath correctly. have the following trivial xml. <?:xml version="1.0" standalone="yes" ?> <top attr="1"> content1 content2 <inner id="first" day="friday"> content3 </inner> </top>
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.