Groups | Blog | Home
all groups > c# > january 2005 >

c# : XPath Question



Nick Malik [Microsoft]
1/17/2005 10:34:54 PM
your first two queries are not difficult:

[quoted text, click to view]

//tblItem[ID=1]

[quoted text, click to view]

//tblItem[Category='Miscellaneous']

[quoted text, click to view]
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.
--
[quoted text, click to view]

Nick Malik [Microsoft]
1/17/2005 10:36:52 PM
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.
--
[quoted text, click to view]

Madhu[C#-MVP]
1/17/2005 10:39:01 PM
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

[quoted text, click to view]
Michael C#
1/17/2005 11:12:09 PM
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


Nick Malik [Microsoft]
1/18/2005 12:42:10 AM
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.
--
[quoted text, click to view]

Nick Malik [Microsoft]
1/18/2005 7:53:36 AM
try these:
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconManageNamespacesUsingXmlNamespaceManager.asp
http://msdn.microsoft.com/library/en-us/dnxmlnet/html/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.
--
[quoted text, click to view]

Michael C
1/18/2005 10:43:48 AM
Now that sounds like it hit the nail on the head. Where can I find out more
about the namespace manager?

Thanks

[quoted text, click to view]

Michael C#
1/18/2005 8:53:52 PM
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

[quoted text, click to view]

Michael C#
1/18/2005 9:10:09 PM
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

[quoted text, click to view]

Michael C#
1/18/2005 10:23:56 PM
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!


[quoted text, click to view]

Mike Schilling
3/9/2005 1:29:11 PM

[quoted text, click to view]

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 .

AddThis Social Bookmark Button