xmlvb.netunicodeutf-8xmltextreader

VB.net Read XML file with wrong encoding


I've been looking for several hours now with no success!

I have a XML file (which a program creates) and I like to get information out of it. Now, my problem is, the header of the files sais UFTF-8, but the file is encoded in UNICODE! VB.net XmlTextReader won't read that file...!? As soon as it gets to "Load", it throws around with exceptions. Then I opened one of those thousands XML files in Notepad++ and saved it as UFT-8 - well, guess that! That file worked!

But I don't think I want to change all the files on our server (new ones are added daily!) and I don't think I can get the developer to change the way he saves those XML-files.

Any ideas on how to "cheat" VB.net into reading those files anyways?

Thanks!


Solution

  • You can change the encoding when you read the file into the memory

    Dim Stream As New IO.StreamReader("File.xml", System.Text.Encoding.UTF8)
    Dim Reader As New Xml.XmlTextReader(Stream)
    

    for a more advanced approach, you can first detect the encoding of the file and then try to change it.