Why "Data at the root level is invalid. Line 1, position 1." for XML Document?
I eventually figured out there was a byte mark exception and removed it using this code:
string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()); if (xml.StartsWith(_byteOrderMarkUtf8)) { var lastIndexOfUtf8 = _byteOrderMarkUtf8.Length-1; xml = xml.Remove(0, lastIndexOfUtf8); }
I can give you two advices:
- It seems you are using "LoadXml" instead of "Load" method. In some cases, it helps me.
- You have an encoding problem. Could you check the encoding of the XML file and write it?
Remove everything before <?xml version="1.0" encoding="utf-8"?>
Sometimes, there is some "invisible" (not visible in all text editors). Some programs add this.
It's called BOM, you can read more about it here: https://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding