473,395 Members | 1,863 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,395 software developers and data experts.

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

Similar topics

2
by: arvindsd | last post by:
Hi all, The code below gets me a list of all the nodes within the node object called xml_tags_root. NodeList nl = xml_tags_root.getChildNodes(); The length of nodelist I get is double the...
7
by: steve bull | last post by:
I have the following code snippet to read the colorRange attributes for the colorRangeSwatch in the xml file listed below. string expr = "/swatches/colorRangeSwatch/colorRange";...
0
by: Dave Boal | last post by:
I am not getting any nodes back with the following XPath Query. Please look at the XML that follows the code: XmlTextReader xtr = new XmlTextReader(this.fileName); XmlDocument xd = new...
5
by: Ben R. | last post by:
Hi, Could someone explain the functional difference between these classes? From what I understand, xpathdocument is faster in some scenarios, but I'm not sure why. Further, why is it that the...
0
by: Michael C# | last post by:
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...
0
by: DeadAtBirth | last post by:
I have an XML file which have nodes that contain filepaths, e.g.<path>c:\SomeDirectory\SomeFile.txt</path> I'm using an XmlDocument and XPath query to try and find a node XmlDocument doc = new...
2
by: george | last post by:
Hi, I have the following node within a xml document. <Results> <Result> <RecordID>1</RecordID> ... </Result> <Result>
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
4
by: =?Utf-8?B?TTFpUw==?= | last post by:
I’m trying to pull up a NodeList of all the Grant elements in the AccessControlList element of the xml document displayed below. However I’m not getting the list using the following code: ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.