2

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?

2
  • 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. Commented Aug 16, 2018 at 9:36
  • 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). Commented Aug 16, 2018 at 9:39

1 Answer 1

5

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.