![]() |
|
|||
|
I have been sending web requests to Google Maps and successfully receiving the expected responses in xml format. I no problem with using an instance of the xmlreader to interpret the response . Sometimes, in these responses it is necessary that I receive characters like a small letter 'o' with the diaeresis. This is necessary because these are usually European names that use characters like this. 'ö' When I get a response like that I get an error message that says "Invalid character in the given encoding. Line 1, position 126.".
How do change the encoding for the xmltextreader? It is my understanding that xmlTextReader instance will use the document's encoding which is listed in the response as UTF-8. I have no idea how to solve this and help would be greatly appreciated. Thanks Last edited by Slowjim; 10-04-2008 at 3:12 PM. |
|
||||
|
Quote:
"ö" in an Utf-8 encoded Xml is not a problem in my experience. I didn't find any GM Xml interface, so this is as far as we get without you posting your code and sample data.
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
Thanks very much for your response.
The error is; Invalid character in the given encoding. Line 1, position 126. Below is what is sent; URL = "http://maps.google.com/maps/geo?q=Hartmannstr+30,Weil+im+Schönbuch,,DE&output= xml&key=" + _ "ABQIAAAAR4h9tcbJyNCgHr0RBAyqDBT2yXp_ZAY8_ufC3CFXh HIE1NvwkxSLYTrTqnjv8HjmG0WWkFrM9nBdOw" This is the offending code; The error occurs when strTmp = XmlRdr.ReadString Code:
XmlRdr = New XmlTextReader(URL)
XmlRdr.WhitespaceHandling = WhitespaceHandling.None
XmlRdr.MoveToContent()
Do While Not XmlRdr.EOF
If XmlRdr.Name = "name" Then
strTmp = XmlRdr.ReadString
End If
If XmlRdr.Name = "code" Then
If Not XmlRdr.ReadString = "200" Then
errFlg = True
strTmp = String.Concat(strTmp, vbCrLf, vbCrLf, "Address not found!")
MsgBox(strTmp, MsgBoxStyle.Exclamation)
End If
Exit Do
End If
XmlRdr.Read()
Loop
URL = "http://maps.google.com/maps/geo?q=7310+Corsicana,Houston,,US&output=xml&key=" + _ "ABQIAAAAR4h9tcbJyNCgHr0RBAyqDBT2yXp_ZAY8_ufC3CFXh HIE1NvwkxSLYTrTqnjv8HjmG0WWkFrM9nBdOw" When I use IExplorer and just paste the offending url in it, this is what is returned. HTML Code:
<?xml version="1.0" encoding="UTF-8" ?> <kml xmlns="http://earth.google.com/kml/2.0"> <Response> <name>Hartmannstr 30,Weil im Schönbuch,,DE</name> <Status> <code>200</code> <request>geocode</request> </Status> <Placemark id="p1"> <address>Hartmannstraße 30, 71093 Weil im Schönbuch, Bundesrepublik Deutschland</address> <AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"> <Country> <CountryNameCode>DE</CountryNameCode> <AdministrativeArea> <AdministrativeAreaName>Baden-Württemberg</AdministrativeAreaName> <SubAdministrativeArea> <SubAdministrativeAreaName>Böblingen</SubAdministrativeAreaName> <Locality> <LocalityName>Weil im Schönbuch</LocalityName> <Thoroughfare> <ThoroughfareName>Hartmannstraße 30</ThoroughfareName> </Thoroughfare> <PostalCode> <PostalCodeNumber>71093</PostalCodeNumber> </PostalCode> </Locality> </SubAdministrativeArea> </AdministrativeArea> </Country> </AddressDetails> <Point> <coordinates>9.056498,48.624399,0</coordinates> </Point> </Placemark> </Response> </kml> Last edited by Slowjim; 10-05-2008 at 7:42 AM. |
|
||||
|
That is very strange, utf-8 is detected. When saving this to file I see there is no file encoding prefix (BOM), and there shouldn't need to be from Xml point of view. If I open it in Notepad and save as utf-8 a BOM is added, if I then use XmlTextReader from this file it reads correctly. I think this must be a bug. Workaround could be this:
Code:
Dim web As New Net.WebClient
My.Computer.FileSystem.WriteAllText("temp4.xml", web.DownloadString(URL), False, System.Text.Encoding.UTF8)
Dim XmlRdr = New XmlTextReader("temp4.xml")
__________________
Some useful links: Learning videoes, Code Samples, WMI Code Creator, MSDN, The Code Project, WindowsClient.net, ASP.net, W3 Schools, Regular-Expressions.info, GDI+ FAQ
How to format posts with code blocks etc - present the problem/post properly ![]() |
|
|||
|
All I can say is that I am very grateful. Your workaround performs perfectly. I guess we'll never know for sure why it wasn't working before. I have also learned something useful in that if it ever happens again I'll remember there is more than one way to skin a cat.
Thanks |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|