Connecting Tech Pros Worldwide Help | Site Map

Read Xml data problem

Newbie
 
Join Date: Mar 2009
Posts: 5
#1: Mar 26 '09
Hi all, this is my first post, i have a little problem.

I'm tryign to read a specific atribute from a xml website using this code:

...
Expand|Select|Wrap|Line Numbers
  1. 'this is the url im trying to read, tmpIn is a string that complete the url:
  2. Dim url As String = "http://maps.google.com/maps/geo?q=" & tmpIn & "&output=xml"
  3.  
  4. Dim reader As New XmlTextReader(url)
  5.  
  6. reader.WhitespaceHandling = WhitespaceHandling.Significant
  7.  
  8. While (reader.Read())
  9.         'now i try to read the atribute coordinates from the url to a string tmpLatLong:
  10.             If reader.Name.ToString() = "coordinates" Then
  11.                 tmpLatLong = reader.ReadString().ToString()
  12.             End If
  13.  
  14.  End While
  15.  
...

The problem is tmpIn takes diferents values (string) when the function is used and sometimes this works, and othertimes it gives an error (Invalid character in the given encoding. Line 9, position 15.), but is not because of the url, is when it tries to read the coordinates... any ideas how can i fix this? or change to a better code? any help is welcome..

thx in advance
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#2: Mar 26 '09

re: Read Xml data problem


So what you're saying is if the TempIn is properly formated as Coordinates the function works, but if the TempIn is not properly formatted then the function fails?

Do I understand that right?
Newbie
 
Join Date: Mar 2009
Posts: 5
#3: Mar 26 '09

re: Read Xml data problem


Quote:

Originally Posted by tlhintoq View Post

So what you're saying is if the TempIn is properly formated as Coordinates the function works, but if the TempIn is not properly formatted then the function fails?

Do I understand that right?

the coordinates is given by the website, they depend of the TempIn value, but i dont know why it fails, for example if TempIn="canedo,santa maria da feira, portugal" (its a valid adress) it works fine, but if it is TempIn="lobao, santa maria da feira, portugal" (its a valid adress too) it doesnt work and gives that error (Invalid character in the given encoding. Line 9, position 15.) but i checking the website i dont see any cause that could make it happen, and i see the coordinates there with a valid value :S
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#4: Mar 26 '09

re: Read Xml data problem


Ok... Check that I have this right..

You feed the URL to XMLTextReader, which received an XML page back based on the location you give it in the TempIn variable.
When you get the whole XML page back as a reply that goes into the While loop.
When the While loop finally finds a tag of "coordinates" it assigns them to tmpLatLong.

But sometimes it goes awry... at you get an error in line 9, position 15.

So what does line 9 of the response look like in a good value and what does line 9 of a bad run look like? What is at position 15 of the bad value?
tlhintoq's Avatar
Moderator
 
Join Date: Mar 2008
Location: Arizona, USA
Posts: 1,745
#5: Mar 26 '09

re: Read Xml data problem


I do notice that
http://maps.google.com/maps/geo?q=ca...gal&output=xml
Gives a response where no characters have accented characters in it.
canedo,santa maria da feira, portugal 200 geocode Canedo, Santa Maria da Feira, Portugal PTPortugalAveiroSanta Maria da FeiraCanedo -8.4630566,41.0118083,0

http://maps.google.com/maps/geo?q=lo...gal&output=xml
On the other hand does have accented a with tilde over it in a couple places.
lobao, santa maria da feira, portugal 200 geocode Lobão, Santa Maria da Feira, Portugal PTPortugalAveiroSanta Maria da FeiraLobão -8.4894589,40.9842033,0

I would bet those are your illegal characters.
Newbie
 
Join Date: Mar 2009
Posts: 5
#6: Mar 26 '09

re: Read Xml data problem


Quote:

Originally Posted by tlhintoq View Post

Ok... Check that I have this right..

You feed the URL to XMLTextReader, which received an XML page back based on the location you give it in the TempIn variable.
When you get the whole XML page back as a reply that goes into the While loop.
When the While loop finally finds a tag of "coordinates" it assigns them to tmpLatLong.

But sometimes it goes awry... at you get an error in line 9, position 15.

So what does line 9 of the response look like in a good value and what does line 9 of a bad run look like? What is at position 15 of the bad value?

yes is how you say :)

well, for example of a non working case, where line 9 is:
Expand|Select|Wrap|Line Numbers
  1. <address>Lobão, Santa Maria da Feira, Portugal</address>
where in the line 21 i have
Expand|Select|Wrap|Line Numbers
  1. <coordinates>-8.4894589,40.9842033,0</coordinates>
that is the information i need...
idk if it is bcause of the char "ã", but i cant change that.

a working case would be like:
Expand|Select|Wrap|Line Numbers
  1. <address>Canedo, Santa Maria da Feira, Portugal</address>
and in line 21
Expand|Select|Wrap|Line Numbers
  1. <coordinates>-8.4630566,41.0118083,0</coordinates>
but im not sure if that kind of characters are the cause of the error
Newbie
 
Join Date: Mar 2009
Posts: 5
#7: Mar 26 '09

re: Read Xml data problem


Quote:

Originally Posted by tlhintoq View Post

I do notice that
http://maps.google.com/maps/geo?q=ca...gal&output=xml
Gives a response where no characters have accented characters in it.
canedo,santa maria da feira, portugal 200 geocode Canedo, Santa Maria da Feira, Portugal PTPortugalAveiroSanta Maria da FeiraCanedo -8.4630566,41.0118083,0

http://maps.google.com/maps/geo?q=lo...gal&output=xml
On the other hand does have accented a with tilde over it in a couple places.
lobao, santa maria da feira, portugal 200 geocode Lobão, Santa Maria da Feira, Portugal PTPortugalAveiroSanta Maria da FeiraLobão -8.4894589,40.9842033,0

I would bet those are your illegal characters.

maybe, but how i ignore that?
Newbie
 
Join Date: Mar 2009
Posts: 5
#8: Mar 26 '09

re: Read Xml data problem


Ok i fixed the problem :D

i added to the url &oe=utf-8, looking like this:
Dim url As String = "http://maps.google.com/maps/geo?q=" & tmpIn & "&output=xml&oe=utf-8"

and solved my problem.

Thx for the help :)
Reply