I’m trying to import an xml file into vb.net XmlDocument but am getting the error:
‘.’, hexadecimal value 0x00, is an invalid character. Line 94, position 1.
I’m wondering if there is a way to replace the hex character 0x00
The following are lines 92,93,94 the file ends on line 94
92 | </request>
93 |</rpc> <!-- XML Finishes here -->
94 |
Thanks for any help.
EDIT: adding code used to get the file.
Dim fs As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read)
Dim buffer(fs.Length) As Byte
fs.Read(buffer, 0, fs.Length)
Dim xmlString As String = System.Text.UTF8Encoding.UTF8.GetString(buffer)
fs.close()
Doc.LoadXml(xmlString.Trim)
I am using System.Text.UTF8Encoding.UTF8.GetString(buffer) because the file encoding is not always UTF-8. Unfortunatly I don’t have controll over the xml file as we are receiving it from an external source who wont change the way the file is generated as it is used by others.
What I want to do is basically get the file into the string then either chop off the end of it from the last > and then append my own > or just replace the HEX character with an empty string.
Okay, to start with your code for reading a file is broken. It will usually work, but you should pretty much never ignore the return value from
Stream.Read. You should also close streams using aUsingstatement orFinallyblock. Fortunately, there’s an incredibly easy way of replacing your code:On the other hand, you claim that the encoding isn’t always UTF-8 – so why are you always trying to use UTF-8? It would actually be better if you loaded it as plain bytes:
or more easily:
Now, if you do that do you still get the same error? If so, the file itself is broken…