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.
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.
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!
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.