XMLReader Encoding

Slowjim

Member
Joined
Sep 8, 2008
Messages
6
Location
League City, TX
Programming Experience
10+
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:
help said:
The encoding declaration, <?xml version="1.0" encoding="ISO-8859-5"?>, contains an encoding attribute that sets the encoding for your document. The XmlTextReader has an Encoding property that returns the character encoding found in the encoding attribute in the XML declaration. If no encoding attribute is found, the default for the document is set to UTF-8 or UTF-16 based on the first two bytes of the stream.
Another thing to know is that if the xml file doesn't specify an encoding it must supply Utf-8 data, which is the default Xml encoding.

"ö" 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.
 
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_ufC3CFXhHIE1NvwkxSLYTrTqnjv8HjmG0WWkFrM9nBdOw"

This is the offending code;
The error occurs when strTmp = XmlRdr.ReadString

VB.NET:
       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
However, if the below url is sent then it works just fine!

URL = "http://maps.google.com/maps/geo?q=7310+Corsicana,Houston,,US&output=xml&key=" + _
"ABQIAAAAR4h9tcbJyNCgHr0RBAyqDBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSLYTrTqnjv8HjmG0WWkFrM9nBdOw"

When I use IExplorer and just paste the offending url in it, this is what is returned.
HTML:
<?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>

I am at a loss to explain this. I would greatly appreciate any help.
 
Last edited:
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:
VB.NET:
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")
 
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
 
Back
Top