Search code examples
xmlvb.netutf-8special-characters

vb net 2017 error when reading xml file


I created a program were I get xml reponses from google maps. Everything went fine, the respose is OK, I can proper write the xml file, and I can read a specific xml node as well, exept if there is some special character in the xml file. In my case when there is an " ë " in "België" in the file.

What can be done, to be able to read xml files with these type of characters in it. Changing the " ë " in " e " can be a solution, but there will other characters giving this issue as well. I'm looking for a solution so I can use the original response from google.

Thanks a lot

my code looks as follows

write the file

    WriteXMLResponseFile(strResult)

read specific node

    strStatus = ReadXMLData("/DistanceMatrixResponse/status")

de code for writing and reading

Public Sub WriteXMLResponseFile(ByVal responseData As String, Optional ByVal fileName As String = "\Data\Response.xml")
    Dim intFileNr As Integer
    intFileNr = FreeFile()
    Try
        FileOpen(intFileNr, Application.StartupPath & fileName, OpenMode.Output)
        PrintLine(intFileNr, responseData)
    Catch ex As Exception
        MessageBox.Show("The file :" & vbCrLf & Application.StartupPath & fileName & vbCrLf & "does not exist", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
    End Try
    FileClose(intFileNr)
End Sub

Function ReadXMLData(ByVal dataNode As String, Optional ByVal fileName As String = "\Data\Response.xml") As String

    Dim xDoc As XmlDocument = New XmlDocument()

    Try
        xDoc.Load(Application.StartupPath & fileName)
        ReadXMLData = xDoc.SelectSingleNode(dataNode).InnerText
    Catch ex As Exception
        MsgBox(ex.Message)

    End Try

End Function

The XML file looks as follows

<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
 <status>OK</status>
 <origin_address>Henleykaai, 9000 Gent, Belgi뼯origin_address>
 <destination_address>Nieuwstraat, 1000 Brussel, Belgi뼯destination_address>
 <row>
  <element>
   <status>OK</status>
   <duration>
    <value>2709</value>
    <text>45 min.</text>
   </duration>
   <distance>
    <value>54752</value>
    <text>54,8 km</text>
   </distance>
   <duration_in_traffic>
    <value>2504</value>
    <text>42 min.</text>
   </duration_in_traffic>
  </element>
 </row>
</DistanceMatrixResponse>

Solution

  • Are you certain the issue is in reading the file and where are you seeing the characters improperly?

    My theory is the issue lies with your calls to PrintLine (which is how I write to files in VB6 - but it's deprecated).

    Try this instead,

    Public Sub WriteXMLResponseFile(ByVal responseData As String, Optional ByVal fileName As String = "\Data\Response.xml")
        Dim intFileNr As Integer
        intFileNr = FreeFile()
        Try
            'FileOpen(intFileNr, Application.StartupPath & fileName, OpenMode.Output)
               'PrintLine(intFileNr, responseData)
            Dim writer As StreamWriter = File.CreateText("C:\TestOutputPath.txt")
             writer.WriteLine(responseData)
             writer.Close
        Catch ex As Exception
            MessageBox.Show("The file :" & vbCrLf & Application.StartupPath & fileName & vbCrLf & "does not exist", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
        End Try
        FileClose(intFileNr)
    End Sub
    

    With this change, checkout "C:\TestOutputPath.txt" in an advanced editor as Notepad will goof up those characters too.