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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29915467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:22:57  来源:igfitidea点击:

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

c#xmlcharacter-encodingxsdxmlreader

提问by user3122648

I am writing an XML validator with XSD.

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

Below is what I did, but when the validator reached the line while (list.Read())it gives me the error

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

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

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

Can anybody help me fix it?

有人可以帮我解决吗?

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

<?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

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>

采纳答案by kjhughes

The reality of your file's encoding appears to conflict with that specified by your XML declaration. If your file actually uses one-byte characters, declaring encoding="utf-16"won't change it to use two-byte characters, for example.

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

Try removing the conflicting encoding from the XML declaration. Replace

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

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

with

<?xml version="1.0"?>

You may also be able to load the file into a string as a work-around using LoadXML().

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

回答by lucky

If you are not able tochange the xml file encoding as

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

<?xml version="1.0"?>

Alternatively, you can read the xml content directly as raw xmlinstead of loading itwith xml path.

或者,您可以直接将 xml 内容作为原始 xml读取,而不是使用 xml 路径加载它

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

If you use XmlDocument;

如果你使用XmlDocument;

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