Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > Database > XML

XML All about XML here

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-04-2008, 2:47 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Location: League City, TX
Age: 62
Posts: 6
Reputation: 0
Slowjim is on a distinguished programming path ahead
Default XMLReader Encoding

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 10-04-2008, 7:53 PM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

Quote:
Originally Posted by help
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.
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 10-04-2008, 9:53 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Location: League City, TX
Age: 62
Posts: 6
Reputation: 0
Slowjim is on a distinguished programming path ahead
Default

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
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_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>
I am at a loss to explain this. I would greatly appreciate any help.

Last edited by Slowjim; 10-05-2008 at 7:42 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 10-05-2008, 8:44 AM
JohnH's Avatar
VB.NET Forum Moderator
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Dec 2005
Location: Norway
Age: 37
Posts: 10,325
Reputation: 1315
JohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond reputeJohnH has a reputation beyond repute
Default

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")
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 10-05-2008, 7:05 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Sep 2008
Location: League City, TX
Age: 62
Posts: 6
Reputation: 0
Slowjim is on a distinguished programming path ahead
Default XMLReader Encoding

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 5:46 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.