473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XPath Question

OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to
be working though...

TIA

Nov 16 '05 #1
10 2259
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1),
//tblItem[ID=1]
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
//tblItem[Category='Miscellaneous']
3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word 'Remote' in any position)
I don't know this one.
One thing that you have to keep in mind, if you want to do this in code, is
that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If
you don't provide the namespace manager, then your query will ALWAYS return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga... OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to be working though...

TIA

Nov 16 '05 #2
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:Gb********************@comcast.com...
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),


//tblItem[ID=1]
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')


//tblItem[Category='Miscellaneous']
3) All tblItem ndoes that have the word Remote in the Description (i.e.,

all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.
One thing that you have to keep in mind, if you want to do this in code,

is that you have to set the namespace manager for the xpath query to include
the defined namespaces. In your example, you have a default namespace. If you don't provide the namespace manager, then your query will ALWAYS return zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga...
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,

all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e.,

all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them

seem to
be working though...

TIA


Nov 16 '05 #3
Hi Michael,

Since you have elements instead of attributes in your XML, your xpath query
i.e. tblItem[ID='Miscellaneous'] will not work since ID in this case is a
element and not a attribute. This type of XPath query can be done only for
attributes.
To make it work, you need to use the text() function. the XPath query for
your three scenarios are as follows,

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1)

/DataSet1/tblItem//ID[text()='1']

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

/DataSet1/tblItem//Category[text()='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

/DataSet1/tblItem//Description[contains(text(),'Remote')]

For more information abt Xpath queries... look at the below url...

<http://www.developer.com/net/net/article.php/11087_3383961_1>

HTH

Regards,
Madhu

MVP - C# | MCSD.NET

"Michael C#" wrote:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good.
I run an XPath query against it to retrieve all the field names. Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to
be working though...

TIA

Nov 16 '05 #4
The two queries I posted worked for me, using XPath Xpress (downloaded off
of gotdotnet.com)
//tblItem[Category='Miscellaneous']
this one works as long as the namespace is right.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Madhu[C#-MVP]" <Ma*******@discussions.microsoft.com> wrote in message
news:EF**********************************@microsof t.com...
Hi Michael,

Since you have elements instead of attributes in your XML, your xpath query i.e. tblItem[ID='Miscellaneous'] will not work since ID in this case is a
element and not a attribute. This type of XPath query can be done only for
attributes.
To make it work, you need to use the text() function. the XPath query for
your three scenarios are as follows,

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1)

/DataSet1/tblItem//ID[text()='1']

2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')

/DataSet1/tblItem//Category[text()='Miscellaneous']

3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word 'Remote' in any position)

/DataSet1/tblItem//Description[contains(text(),'Remote')]

For more information abt Xpath queries... look at the below url...

<http://www.developer.com/net/net/article.php/11087_3383961_1>

HTH

Regards,
Madhu

MVP - C# | MCSD.NET

"Michael C#" wrote:
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the field names. Everything there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath queries
that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description (i.e., all tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing XPath
expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them seem to be working though...

TIA

Nov 16 '05 #5
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:cN********************@comcast.com...
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:Gb********************@comcast.com...
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,
all
tblItem where ID = 1),


//tblItem[ID=1]
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')


//tblItem[Category='Miscellaneous']
3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
all
tblItem where Category contains the word 'Remote' in any position)

I don't know this one.
One thing that you have to keep in mind, if you want to do this in code,

is
that you have to set the namespace manager for the xpath query to include the defined namespaces. In your example, you have a default namespace.

If
you don't provide the namespace manager, then your query will ALWAYS

return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga...
OK, here's the deal. I have a small XML file that represents a small
database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the field names.

Everything
there works fine.

Here's my XML Document:

<?xml version="1.0" standalone="yes" ?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<tblItem>
<ID>1</ID>
<Name>Spam</Name>
<Category>Food</Category>
<Description>Yummy! No natural ingredients</Description>
<Price>4</Price>
<ImageURL>images/1.png</ImageURL>
<LargeImageURL>images/L1.png</LargeImageURL>
</tblItem>
<tblItem>
<ID>2</ID>
<Name>Remote Control</Name>
<Category>Miscellaneous</Category>
<Description>Universal Remote</Description>
<Price>12</Price>
<ImageURL>images/2.png</ImageURL>
<LargeImageURL>images/L2.png</LargeImageURL>
</tblItem>
</DataSet>

Now for the tricky part. I'm trying to come up with three XPath

queries that will return the following:

1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
tblItem where ID = 1),
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')
3) All tblItem ndoes that have the word Remote in the Description
(i.e., all
tblItem where Category contains the word 'Remote' in any position)

Coming from a SQL background, I'm having a hard time implementing

XPath expressions. I was hoping someone here could point me in the right
direction. I've tried several combinations, like "tblItem/ID[.='1']",
"tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them

seem
to
be working though...

TIA



Nov 16 '05 #6
try these:
http://msdn.microsoft.com/library/en...aceManager.asp
http://msdn.microsoft.com/library/en...xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C" <me@mine.com> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
Now that sounds like it hit the nail on the head. Where can I find out more about the namespace manager?

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:cN********************@comcast.com...
one more thing: a good link with a tutorial on XPath queries is:
http://www.w3schools.com/xpath/xpath_syntax.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:Gb********************@comcast.com...
your first two queries are not difficult:

> 1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
> tblItem where ID = 1),

//tblItem[ID=1]

> 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > tblItem where Category = 'Miscellaneous')

//tblItem[Category='Miscellaneous']

> 3) All tblItem ndoes that have the word Remote in the Description (i.e., all
> tblItem where Category contains the word 'Remote' in any position)
>
I don't know this one.
One thing that you have to keep in mind, if you want to do this in code,
is
that you have to set the namespace manager for the xpath query to include the defined namespaces. In your example, you have a default
namespace.
If
you don't provide the namespace manager, then your query will ALWAYS

return
zero results.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C#" <xy*@abcdef.com> wrote in message
news:65*****************@fe08.lga...
> OK, here's the deal. I have a small XML file that represents a

small > database table. I load it into a System.XML.XMLDocument. So far so

good.
> I run an XPath query against it to retrieve all the field names.
Everything
> there works fine.
>
> Here's my XML Document:
>
> <?xml version="1.0" standalone="yes" ?>
> <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
> <tblItem>
> <ID>1</ID>
> <Name>Spam</Name>
> <Category>Food</Category>
> <Description>Yummy! No natural ingredients</Description>
> <Price>4</Price>
> <ImageURL>images/1.png</ImageURL>
> <LargeImageURL>images/L1.png</LargeImageURL>
> </tblItem>
> <tblItem>
> <ID>2</ID>
> <Name>Remote Control</Name>
> <Category>Miscellaneous</Category>
> <Description>Universal Remote</Description>
> <Price>12</Price>
> <ImageURL>images/2.png</ImageURL>
> <LargeImageURL>images/L2.png</LargeImageURL>
> </tblItem>
> </DataSet>
>
> Now for the tricky part. I'm trying to come up with three XPath

queries > that will return the following:
>
> 1) All tblItem nodes that have a child ID node with a value of 1 (i.e., all
> tblItem where ID = 1),
> 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > tblItem where Category = 'Miscellaneous')
> 3) All tblItem ndoes that have the word Remote in the Description (i.e., all
> tblItem where Category contains the word 'Remote' in any position)
>
> Coming from a SQL background, I'm having a hard time implementing XPath > expressions. I was hoping someone here could point me in the right
> direction. I've tried several combinations, like "tblItem/ID[.='1']", > "tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of them

seem
to
> be working though...
>
> TIA
>
>
>



Nov 16 '05 #7
Hey Nick,

Based on my previous XML I tried the following code:

Dim XDoc As New System.Xml.XmlDocument
Dim XPathQuery As String = "//tblItem[Category='Miscellaneous']"
XDoc.Load(Server.MapPath("StoreItems.xml"))
Dim XMLResults As System.Xml.XmlNodeList = XDoc.SelectNodes(XPathQuery)

XMLResults consistently returns 0 nodes, regardless of the XPathQuery value
I pass to it. Any ideas why it's returning nothing? I started playing with
a Namespace Manager also... I'm using the default namespace, do I really
need it? I also tried the queries posted by Madhu with the same results.

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:6f********************@comcast.com...
try these:
http://msdn.microsoft.com/library/en...aceManager.asp
http://msdn.microsoft.com/library/en...xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C" <me@mine.com> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
Now that sounds like it hit the nail on the head. Where can I find out

more
about the namespace manager?

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:cN********************@comcast.com...
> one more thing: a good link with a tutorial on XPath queries is:
> http://www.w3schools.com/xpath/xpath_syntax.asp
>
> --
> --- Nick Malik [Microsoft]
> MCSD, CFPS, Certified Scrummaster
> http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
> I do not answer questions on behalf of my employer. I'm just a
> programmer helping programmers.
> --
> "Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in
> message
> news:Gb********************@comcast.com...
> > your first two queries are not difficult:
> >
> > > 1) All tblItem nodes that have a child ID node with a value of 1

(i.e.,
> > all
> > > tblItem where ID = 1),
> >
> > //tblItem[ID=1]
> >
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > > > tblItem where Category = 'Miscellaneous')
> >
> > //tblItem[Category='Miscellaneous']
> >
> > > 3) All tblItem ndoes that have the word Remote in the Description

(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > I don't know this one.
> >
> >
> > One thing that you have to keep in mind, if you want to do this in code, > is
> > that you have to set the namespace manager for the xpath query to

include
> > the defined namespaces. In your example, you have a default namespace. > If
> > you don't provide the namespace manager, then your query will ALWAYS
> return
> > zero results.
> >
> > --
> > --- Nick Malik [Microsoft]
> > MCSD, CFPS, Certified Scrummaster
> > http://blogs.msdn.com/nickmalik
> >
> > Disclaimer: Opinions expressed in this forum are my own, and not
> > representative of my employer.
> > I do not answer questions on behalf of my employer. I'm just a
> > programmer helping programmers.
> > --
> > "Michael C#" <xy*@abcdef.com> wrote in message
> > news:65*****************@fe08.lga...
> > > OK, here's the deal. I have a small XML file that represents a small > > > database table. I load it into a System.XML.XMLDocument. So far
> > > so
> good.
> > > I run an XPath query against it to retrieve all the field names.
> > Everything
> > > there works fine.
> > >
> > > Here's my XML Document:
> > >
> > > <?xml version="1.0" standalone="yes" ?>
> > > <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
> > > <tblItem>
> > > <ID>1</ID>
> > > <Name>Spam</Name>
> > > <Category>Food</Category>
> > > <Description>Yummy! No natural ingredients</Description>
> > > <Price>4</Price>
> > > <ImageURL>images/1.png</ImageURL>
> > > <LargeImageURL>images/L1.png</LargeImageURL>
> > > </tblItem>
> > > <tblItem>
> > > <ID>2</ID>
> > > <Name>Remote Control</Name>
> > > <Category>Miscellaneous</Category>
> > > <Description>Universal Remote</Description>
> > > <Price>12</Price>
> > > <ImageURL>images/2.png</ImageURL>
> > > <LargeImageURL>images/L2.png</LargeImageURL>
> > > </tblItem>
> > > </DataSet>
> > >
> > > Now for the tricky part. I'm trying to come up with three XPath

queries
> > > that will return the following:
> > >
> > > 1) All tblItem nodes that have a child ID node with a value of 1

(i.e.,
> > all
> > > tblItem where ID = 1),
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > > > tblItem where Category = 'Miscellaneous')
> > > 3) All tblItem ndoes that have the word Remote in the Description

(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > > Coming from a SQL background, I'm having a hard time implementing

XPath
> > > expressions. I was hoping someone here could point me in the right
> > > direction. I've tried several combinations, like "tblItem/ID[.='1']", > > > "tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
> > > them
> seem
> > to
> > > be working though...
> > >
> > > TIA
> > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #8
Oops, that's VB.NET. Here's the C#.NET version:

System.Xml.XmlDocument XDoc = New System.Xml.XmlDocument();
string XPathQuery = "//tblItem[Category='Miscellaneous']";
XDoc.Load(Server.MapPath("StoreItems.xml"));
System.Xml.XmlNodeList XMLResults = XDoc.SelectNodes(XPathQuery);

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:6f********************@comcast.com...
try these:
http://msdn.microsoft.com/library/en...aceManager.asp
http://msdn.microsoft.com/library/en...xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C" <me@mine.com> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
Now that sounds like it hit the nail on the head. Where can I find out

more
about the namespace manager?

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:cN********************@comcast.com...
> one more thing: a good link with a tutorial on XPath queries is:
> http://www.w3schools.com/xpath/xpath_syntax.asp
>
> --
> --- Nick Malik [Microsoft]
> MCSD, CFPS, Certified Scrummaster
> http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
> I do not answer questions on behalf of my employer. I'm just a
> programmer helping programmers.
> --
> "Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in
> message
> news:Gb********************@comcast.com...
> > your first two queries are not difficult:
> >
> > > 1) All tblItem nodes that have a child ID node with a value of 1

(i.e.,
> > all
> > > tblItem where ID = 1),
> >
> > //tblItem[ID=1]
> >
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > > > tblItem where Category = 'Miscellaneous')
> >
> > //tblItem[Category='Miscellaneous']
> >
> > > 3) All tblItem ndoes that have the word Remote in the Description

(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > I don't know this one.
> >
> >
> > One thing that you have to keep in mind, if you want to do this in code, > is
> > that you have to set the namespace manager for the xpath query to

include
> > the defined namespaces. In your example, you have a default namespace. > If
> > you don't provide the namespace manager, then your query will ALWAYS
> return
> > zero results.
> >
> > --
> > --- Nick Malik [Microsoft]
> > MCSD, CFPS, Certified Scrummaster
> > http://blogs.msdn.com/nickmalik
> >
> > Disclaimer: Opinions expressed in this forum are my own, and not
> > representative of my employer.
> > I do not answer questions on behalf of my employer. I'm just a
> > programmer helping programmers.
> > --
> > "Michael C#" <xy*@abcdef.com> wrote in message
> > news:65*****************@fe08.lga...
> > > OK, here's the deal. I have a small XML file that represents a small > > > database table. I load it into a System.XML.XMLDocument. So far
> > > so
> good.
> > > I run an XPath query against it to retrieve all the field names.
> > Everything
> > > there works fine.
> > >
> > > Here's my XML Document:
> > >
> > > <?xml version="1.0" standalone="yes" ?>
> > > <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
> > > <tblItem>
> > > <ID>1</ID>
> > > <Name>Spam</Name>
> > > <Category>Food</Category>
> > > <Description>Yummy! No natural ingredients</Description>
> > > <Price>4</Price>
> > > <ImageURL>images/1.png</ImageURL>
> > > <LargeImageURL>images/L1.png</LargeImageURL>
> > > </tblItem>
> > > <tblItem>
> > > <ID>2</ID>
> > > <Name>Remote Control</Name>
> > > <Category>Miscellaneous</Category>
> > > <Description>Universal Remote</Description>
> > > <Price>12</Price>
> > > <ImageURL>images/2.png</ImageURL>
> > > <LargeImageURL>images/L2.png</LargeImageURL>
> > > </tblItem>
> > > </DataSet>
> > >
> > > Now for the tricky part. I'm trying to come up with three XPath

queries
> > > that will return the following:
> > >
> > > 1) All tblItem nodes that have a child ID node with a value of 1

(i.e.,
> > all
> > > tblItem where ID = 1),
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e., all > > > tblItem where Category = 'Miscellaneous')
> > > 3) All tblItem ndoes that have the word Remote in the Description

(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > > Coming from a SQL background, I'm having a hard time implementing

XPath
> > > expressions. I was hoping someone here could point me in the right
> > > direction. I've tried several combinations, like "tblItem/ID[.='1']", > > > "tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
> > > them
> seem
> > to
> > > be working though...
> > >
> > > TIA
> > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #9
Hey I think I got it. Looks like I need to prefix *everything* with the
namespace; i.e.,

string XPathQuery = "//ns:tblItem/ns:Category[text()='Miscellaneous']";

Looks like it's a combination of both your great info. Thanks guys!
"Michael C#" <xy*@abcdef.com> wrote in message
news:Up*****************@fe08.lga...
Oops, that's VB.NET. Here's the C#.NET version:

System.Xml.XmlDocument XDoc = New System.Xml.XmlDocument();
string XPathQuery = "//tblItem[Category='Miscellaneous']";
XDoc.Load(Server.MapPath("StoreItems.xml"));
System.Xml.XmlNodeList XMLResults = XDoc.SelectNodes(XPathQuery);

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:6f********************@comcast.com...
try these:
http://msdn.microsoft.com/library/en...aceManager.asp
http://msdn.microsoft.com/library/en...xpthviewer.asp

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Michael C" <me@mine.com> wrote in message
news:uc**************@TK2MSFTNGP14.phx.gbl...
Now that sounds like it hit the nail on the head. Where can I find out

more
about the namespace manager?

Thanks

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:cN********************@comcast.com...
> one more thing: a good link with a tutorial on XPath queries is:
> http://www.w3schools.com/xpath/xpath_syntax.asp
>
> --
> --- Nick Malik [Microsoft]
> MCSD, CFPS, Certified Scrummaster
> http://blogs.msdn.com/nickmalik
>
> Disclaimer: Opinions expressed in this forum are my own, and not
> representative of my employer.
> I do not answer questions on behalf of my employer. I'm just a
> programmer helping programmers.
> --
> "Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in
> message
> news:Gb********************@comcast.com...
> > your first two queries are not difficult:
> >
> > > 1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
> > all
> > > tblItem where ID = 1),
> >
> > //tblItem[ID=1]
> >
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e.,

all
> > > tblItem where Category = 'Miscellaneous')
> >
> > //tblItem[Category='Miscellaneous']
> >
> > > 3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > I don't know this one.
> >
> >
> > One thing that you have to keep in mind, if you want to do this in

code,
> is
> > that you have to set the namespace manager for the xpath query to
include
> > the defined namespaces. In your example, you have a default

namespace.
> If
> > you don't provide the namespace manager, then your query will ALWAYS
> return
> > zero results.
> >
> > --
> > --- Nick Malik [Microsoft]
> > MCSD, CFPS, Certified Scrummaster
> > http://blogs.msdn.com/nickmalik
> >
> > Disclaimer: Opinions expressed in this forum are my own, and not
> > representative of my employer.
> > I do not answer questions on behalf of my employer. I'm just a
> > programmer helping programmers.
> > --
> > "Michael C#" <xy*@abcdef.com> wrote in message
> > news:65*****************@fe08.lga...
> > > OK, here's the deal. I have a small XML file that represents a

small
> > > database table. I load it into a System.XML.XMLDocument. So far
> > > so
> good.
> > > I run an XPath query against it to retrieve all the field names.
> > Everything
> > > there works fine.
> > >
> > > Here's my XML Document:
> > >
> > > <?xml version="1.0" standalone="yes" ?>
> > > <DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
> > > <tblItem>
> > > <ID>1</ID>
> > > <Name>Spam</Name>
> > > <Category>Food</Category>
> > > <Description>Yummy! No natural ingredients</Description>
> > > <Price>4</Price>
> > > <ImageURL>images/1.png</ImageURL>
> > > <LargeImageURL>images/L1.png</LargeImageURL>
> > > </tblItem>
> > > <tblItem>
> > > <ID>2</ID>
> > > <Name>Remote Control</Name>
> > > <Category>Miscellaneous</Category>
> > > <Description>Universal Remote</Description>
> > > <Price>12</Price>
> > > <ImageURL>images/2.png</ImageURL>
> > > <LargeImageURL>images/L2.png</LargeImageURL>
> > > </tblItem>
> > > </DataSet>
> > >
> > > Now for the tricky part. I'm trying to come up with three XPath
queries
> > > that will return the following:
> > >
> > > 1) All tblItem nodes that have a child ID node with a value of 1
(i.e.,
> > all
> > > tblItem where ID = 1),
> > > 2) All tblItem nodes that have a Category of Miscellaneous (i.e.,

all
> > > tblItem where Category = 'Miscellaneous')
> > > 3) All tblItem ndoes that have the word Remote in the Description
(i.e.,
> > all
> > > tblItem where Category contains the word 'Remote' in any position)
> > >
> > > Coming from a SQL background, I'm having a hard time implementing
XPath
> > > expressions. I was hoping someone here could point me in the
> > > right
> > > direction. I've tried several combinations, like

"tblItem/ID[.='1']",
> > > "tblItem[ID='1']" and "//tblItem[ID='Miscellaneous']". None of
> > > them
> seem
> > to
> > > be working though...
> > >
> > > TIA
> > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #10

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:Gb********************@comcast.com...
your first two queries are not difficult:
1) All tblItem nodes that have a child ID node with a value of 1 (i.e.,

all
tblItem where ID = 1),


//tblItem[ID=1]
2) All tblItem nodes that have a Category of Miscellaneous (i.e., all
tblItem where Category = 'Miscellaneous')


//tblItem[Category='Miscellaneous']
3) All tblItem ndoes that have the word Remote in the Description (i.e.,

all
tblItem where Category contains the word 'Remote' in any position)


I'm guessing that should read "where Description contains the word 'Remote'
in any position"

If so, it's

//tbItem[contains(Description, "Remote")]

By the way, the XPath 1.0 spec is simple and readable. You can find it at
http://www.w3.org/TR/xpath .
Nov 16 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Sebastian Petzelberger | last post by:
Hi group, please give me an example of a xpath with regex or better a link with examples. Thanks in advance, Sebastian
0
by: Bart | last post by:
Hello, I am real new to using LIBXML and have a question about XPATH evaluations. The question may show a real ignorance of the LIBXML structure, don't so assume I know what I am talking about....
3
by: Kathy Burke | last post by:
Hi again, I'm using the following xpath (works in visualizer) with a SelectSingleNode("xpath") statement. //Station/(WI])]/@order Problem is I get an error "expression passed to this method...
2
by: ree32 | last post by:
When I import an xml document in Visual studio and Genereate as schema from it, and create a dataset from it, it adds this line into to the root element of my xml file -...
9
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...
5
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
7
by: Tim Hallwyl | last post by:
Hi, there! As I understand the XPaht recommendation, the context node is a node; not a node-list, not XPath object -- but a single node. Now, the WS-BPEL 2.0 specification allows an XML simple...
0
by: pompair | last post by:
Hello, I'm making a quiz game for fun. I have an xml file like this: <?xml version="1.0" encoding="utf-8" ?> <results> <index>99</index> <answers>11</answers> <questions> <question id="1">
0
by: John Krukoff | last post by:
On Wed, 2008-09-03 at 13:36 -0700, bruce wrote: Well, you could just do the test (and the count!) in the xpath expression: count( //tr/td ) It sounds like you're not familiar with xpath? I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.