Read Xml data problem | Newbie | | Join Date: Mar 2009
Posts: 5
| |
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:
... -
'this is the url im trying to read, tmpIn is a string that complete the url:
-
Dim url As String = "http://maps.google.com/maps/geo?q=" & tmpIn & "&output=xml"
-
-
Dim reader As New XmlTextReader(url)
-
-
reader.WhitespaceHandling = WhitespaceHandling.Significant
-
-
While (reader.Read())
-
'now i try to read the atribute coordinates from the url to a string tmpLatLong:
-
If reader.Name.ToString() = "coordinates" Then
-
tmpLatLong = reader.ReadString().ToString()
-
End If
-
-
End While
-
...
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
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | 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
| | | re: Read Xml data problem Quote:
Originally Posted by tlhintoq 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
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | 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?
|  | Moderator | | Join Date: Mar 2008 Location: Arizona, USA
Posts: 1,745
| | | 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
| | | re: Read Xml data problem Quote:
Originally Posted by tlhintoq 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: - <address>Lobão, Santa Maria da Feira, Portugal</address>
where in the line 21 i have - <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: - <address>Canedo, Santa Maria da Feira, Portugal</address>
and in line 21 - <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
| | | re: Read Xml data problem Quote:
Originally Posted by tlhintoq 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
| | | 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 :)
|  | Similar Visual Basic .NET bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,272 network members.
|