How to select xml nodes from webservice xml file.

Hello Experts,

As an overview, I needed some way to retrieve timezone by zipcode, so I found a web service that returns an xml file when accessed as so:

http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=+MYZIPCODE+

This is what it returns:

<?xml version="1.0" encoding="utf-8" ?>
- <NewDataSet>
- <Table>
  <CITY>Hayward</CITY>
  <STATE>CA</STATE>
  <ZIP>94541</ZIP>
  <AREA_CODE>510</AREA_CODE>
  <TIME_ZONE>P</TIME_ZONE>
  </Table>
  </NewDataSet>

I need to grab the TIME_ZONE node, but not sure how to....help anyone?

Thanks in advance.
rmartesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
XmlDocument doc = new XmlDocument();
doc.LoadXml("Your xml string");

XmlNode timezone = doc.SelectSingleNode("/NewDataSet/Table/TIME_ZONE");
string tz = timezone.InnerText;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rmartesAuthor Commented:
Thanks Carl,

Its pretty straight forward, but when I tried it, I get a "Data at the root level is invalid. Line 1, position 1."

Any suggestions?
0
Carl TawnSystems and Integration DeveloperCommented:
Can you post what you have? If the XML you posted is precisely what you have then it should be fine. The two hyphens should be there, but I assume they're just there because you copied and pasted from a browser.
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Erick37Commented:
Give this a shot.  It reads the data directly into an XMLNode.

 
ServiceReference1.USZipSoapClient sr = new ServiceReference1.USZipSoapClient();

			XmlNode node = sr.GetInfoByZIP("10018");

			Console.WriteLine(String.Format("{0} {1} {2} {3} {4}"
			,node.SelectSingleNode("Table/CITY").InnerText
			,node.SelectSingleNode("Table/STATE").InnerText
			,node.SelectSingleNode("Table/ZIP").InnerText
			,node.SelectSingleNode("Table/AREA_CODE").InnerText
			,node.SelectSingleNode("Table/TIME_ZONE").InnerText
			));

Open in new window

0
rmartesAuthor Commented:
Here is my code:

XmlDocument doc = new XmlDocument();
doc.LoadXml("http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=10460");

XmlNode timezone = doc.SelectSingleNode("/NewDataSet/Table/TIME_ZONE");
string tz = timezone.InnerText;

Response.Write(tz);

Here is the xml file returned by the web service:

<?xml version="1.0" encoding="utf-8" ?>
<NewDataSet>
 <Table>
  <CITY>Hayward</CITY>
  <STATE>CA</STATE>
  <ZIP>94541</ZIP>
  <AREA_CODE>510</AREA_CODE>
  <TIME_ZONE>P</TIME_ZONE>
 </Table>
</NewDataSet>
0
Erick37Commented:
Your code should work if you use doc.Load() instead of doc.LoadXml()...

doc.Load("http://www.webservicex.net/uszip.asmx/GetInfoByZIP?USZip=10460");
0
rmartesAuthor Commented:
Great
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.