我想使用 XmlDocument.Load(String) 方法从 xml 文件加载 XMLDocument,但是当我尝试使用它时出现此错误:
System.Xml.XmlException:“.”,十六进制值 0x00,无效 特点。 2 号线,位置 1。
当我尝试在 Visual Studio 中打开文件时,文件的编码为 Unicode,Visual Studio 自动切换为 Unicode(UTF-8)。 当我用 *Unicode(UTF-8) 编码保存文件后,程序完美运行。
为什么会发生这种情况?用这种方法可以加载Unicode编码的文件吗?
我能够通过使用 StreamReader 类加载文件内容来解决此问题,然后使用 XmlDocument.Load(Stream) 方法。
这是代码:
XmlDocument xmlDocument = new XmlDocument();
StreamReader reader = new StreamReader(filePath);
xmlDocument.Load(reader);
reader.Close();