没有 Unicode 字节顺序标记。无法切换到 Unicode

There is no Unicode byte order mark. Cannot switch to Unicode

我正在用 XSD 编写一个 XML 验证器。

下面是我所做的,但是当验证器到达行 while (list.Read()) 时,它给了我错误

There is no Unicode byte order mark. Cannot switch to Unicode.

有人可以帮我解决吗?

public class Validator
    {
        public void Validate(string xmlString)
        {
            Boolean bRet = true;
            string xmlPath = @"C:\x.xml";
            string xsdPath = @"C:\general.xsd";

            XmlReaderSettings Settings = new XmlReaderSettings();
            Settings.Schemas.Add("", xsdPath);
            Settings.ValidationType = ValidationType.Schema;
            Settings.ValidationEventHandler += 
               new ValidationEventHandler(SettingsValidationEventHandler);

            XmlReader list = XmlReader.Create(xmlPath, Settings);
            //StringBuilder output = new StringBuilder();
            while (list.Read()) 
            {
            }
            //File.WriteAllText(@"D:\Output.xml", output.ToString());
        }
        static void SettingsValidationEventHandler(object sender,
                                                   ValidationEventArgs e)
        {
            if (e.Severity == XmlSeverityType.Warning)
            {
                MessageBox.Show( "WARNING: ");
                MessageBox.Show(e.Message);
            }
            else if (e.Severity == XmlSeverityType.Error)
            {
                MessageBox.Show("ERROR: ");
                MessageBox.Show(e.Message);
            }
        }
    }

XML

<?xml version="1.0" encoding="utf-16"?>
<FlashList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:xsd="http://www.w3.org/2001/XMLSchema" vin="xxxxxxxxxxxxx">
  <flash ECUtype="xxx" />
</FlashList>

XSD

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" 
           elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="FlashList">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="flash" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="ECUtype" use="optional"/>
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
        <xs:element name="Error" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:simpleContent>
              <xs:extension base="xs:string">
                <xs:attribute type="xs:byte" name="code" use="optional" />
              </xs:extension>
            </xs:simpleContent>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
      <xs:attribute type="xs:string" name="vin"/>
    </xs:complexType>
  </xs:element>
</xs:schema>

您的文件编码的实际情况似乎与您的 XML 声明指定的编码冲突。例如,如果您的文件实际使用单字节字符,则声明 encoding="utf-16" 不会将其更改为使用双字节字符。

尝试从 XML 声明中删除冲突的编码。替换

<?xml version="1.0" encoding="utf-16"?>

<?xml version="1.0"?>

作为解决方法,您也可以使用 LoadXML() 将文件加载到字符串中。

如果您无法将xml文件编码更改为

<?xml version="1.0"?>

或者,您可以直接阅读 xml 原始内容 xml 而不是加载它 与xml路径。

XmlReader.Create(new StringReader(File.ReadAllText(fileName)));

如果你使用XmlDocument;

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(File.ReadAllText(filePath));

当您在 XML 头部声明使用 UTF-16 编码时,会抛出此错误,但实际不以这种编码保存此文件。

你可以使用简单的Windows记事本检查,点击另存为,然后在底部检查xml文件的编码(可能它仍然是UTF-8,而不是UTF-16 ).

Screenshot of notepad encoding setting