0
votes

I want to load a XMLDocument from xml file using XmlDocument.Load(String) method, but i get this error when I try to use it:

System.Xml.XmlException: '.', hexadecimal value 0x00, is an invalid character. Line 2, position 1.

When I tried to open file in Visual Studio, the encoding for the file was Unicode and Visual studio switches automatically to Unicode(UTF-8). After I saved the file with *Unicode(UTF-8) Encoding, the program works perfect.

Why is this happening and it is possible to load Unicode encoded files with this method?

1
If line 1 has anything other than utf8 you have to skip the first line. It is the format of the text file that is causing the issue.jdweng
I don't want to skip any line from xml file. I want to understand why the method is throwing this exception with the encoding and I want to be able to load the xml file even if the encoding is Unicode( if this is possible).Raluca

1 Answers

3
votes

I was able to resolve this problem, by using the StreamReader class to load the content of the file and then I used the XmlDocument.Load(Stream) method.

Here is the code:

 XmlDocument xmlDocument = new XmlDocument();
 StreamReader reader = new StreamReader(filePath);
 xmlDocument.Load(reader);
 reader.Close();