468,256 Members | 1,423 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,256 developers. It's quick & easy.

I can't query an XMLDocument with XPATH; every query returns 0 nodes

Please help! I'm using the following code to get an XML doc:

string str =
"http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison";

System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate(str);
request.Credentials =
System.Net.CredentialCache.DefaultCredentials;

System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream inputStream = response.GetResponseStream();

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(inputStream);

Here's what the XML looks like (or you can query yahoo yourself to look at
it):

<?xml version="1.0" ?>
- <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps
http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
- <Result precision="city">
<Latitude>34.202221</Latitude>
<Longitude>-87.181549</Longitude>
<Address />
<City>ADDISON</City>
<State>AL</State>
<Zip />
<Country>US</Country>
</Result>
- <Result precision="city">
<Latitude>41.71854</Latitude>
<Longitude>-72.577469</Longitude>
<Address />
<City>ADDISON</City>
<State>CT</State>
<Zip />
<Country>US</Country>
</Result>
...and so on

I'm trying to get nodes with doc.SelectNodes(), but EVERY xpath query I use
returns zero nodes. @"//State", @"//Result", @"//ResultSet", you name it,
I've tried it and I get zero nodes each time. What am I doing wrong???

When I run this in the debugger, I can see that the document definitely
loads up just fine. I can see NO reason why I can't query it at all. The
only query that DOES seem to work is @".", which returns one node named
"#document". Everything else returns zero nodes.

Dec 12 '06 #1
3 3826
"0to60" <ho***************@yahoo.comwrote in message
news:DF**********************************@microsof t.com...
Please help! I'm using the following code to get an XML doc:

string str =
"http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison";

System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate(str);
request.Credentials =
System.Net.CredentialCache.DefaultCredentials;

System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream inputStream =
response.GetResponseStream();

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(inputStream);

Here's what the XML looks like (or you can query yahoo yourself to look at
it):

<?xml version="1.0" ?>
- <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps
http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
- <Result precision="city">
<Latitude>34.202221</Latitude>
<Longitude>-87.181549</Longitude>
<Address />
<City>ADDISON</City>
<State>AL</State>
<Zip />
<Country>US</Country>
</Result>
- <Result precision="city">
<Latitude>41.71854</Latitude>
<Longitude>-72.577469</Longitude>
<Address />
<City>ADDISON</City>
<State>CT</State>
<Zip />
<Country>US</Country>
</Result>
...and so on

I'm trying to get nodes with doc.SelectNodes(), but EVERY xpath query I
use returns zero nodes. @"//State", @"//Result", @"//ResultSet", you name
it, I've tried it and I get zero nodes each time. What am I doing
wrong???

When I run this in the debugger, I can see that the document definitely
loads up just fine. I can see NO reason why I can't query it at all. The
only query that DOES seem to work is @".", which returns one node named
"#document". Everything else returns zero nodes.
Are you using the urn:yahoo:maps namespace in your query?

John
Dec 12 '06 #2
Read more about namespaces.

In an XPath expression an unprefixed name is always assumed to belong to
no-namespace.

Names of elements belonging to a namespace (including the default namespace)
must be correctly specified -- prefixed -- in an XPath expression.

Cheers,
Dimitre Novatchev

"0to60" <ho***************@yahoo.comwrote in message
news:DF**********************************@microsof t.com...
Please help! I'm using the following code to get an XML doc:

string str =
"http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison";

System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.C reate(str);
request.Credentials =
System.Net.CredentialCache.DefaultCredentials;

System.Net.HttpWebResponse response =
(System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream inputStream =
response.GetResponseStream();

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(inputStream);

Here's what the XML looks like (or you can query yahoo yourself to look at
it):

<?xml version="1.0" ?>
- <ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:yahoo:maps" xsi:schemaLocation="urn:yahoo:maps
http://api.local.yahoo.com/MapsService/V1/GeocodeResponse.xsd">
- <Result precision="city">
<Latitude>34.202221</Latitude>
<Longitude>-87.181549</Longitude>
<Address />
<City>ADDISON</City>
<State>AL</State>
<Zip />
<Country>US</Country>
</Result>
- <Result precision="city">
<Latitude>41.71854</Latitude>
<Longitude>-72.577469</Longitude>
<Address />
<City>ADDISON</City>
<State>CT</State>
<Zip />
<Country>US</Country>
</Result>
...and so on

I'm trying to get nodes with doc.SelectNodes(), but EVERY xpath query I
use returns zero nodes. @"//State", @"//Result", @"//ResultSet", you name
it, I've tried it and I get zero nodes each time. What am I doing
wrong???

When I run this in the debugger, I can see that the document definitely
loads up just fine. I can see NO reason why I can't query it at all. The
only query that DOES seem to work is @".", which returns one node named
"#document". Everything else returns zero nodes.

Dec 12 '06 #3
the W3C
admitted that the requirement to use qnames when working wihhin namespaces
was counterintuitive, and made XPath1 cumbersome. Now they specify that
localnames match to the default namespace.
That is wrong and plain misinformation.

"andyclap" <an******@discussions.microsoft.comwrote in message
news:34**********************************@microsof t.com...
Does anybody here know if/when MS is planning XPath2 support - the W3C
admitted that the requirement to use qnames when working wihhin namespaces
was counterintuitive, and made XPath1 cumbersome. Now they specify that
localnames match to the default namespace.

Alternatively, does anybody have a little method that'll take an XPath
expression and add a prefix to all localnames?

Dec 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by arvindsd | last post: by
reply views Thread by Dave Boal | last post: by
5 posts views Thread by Ben R. | last post: by
reply views Thread by Michael C# | last post: by
2 posts views Thread by george | last post: by
3 posts views Thread by Jason Mobarak | last post: by
4 posts views Thread by =?Utf-8?B?TTFpUw==?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.