没有Unicode字节顺序标记。无法切换到Unicode [英] There is no Unicode byte order mark. Cannot switch to Unicode

查看:4221
本文介绍了没有Unicode字节顺序标记。无法切换到Unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写与XSD一个XML验证



下面是我做什么,但是当我到达行而(list.Read( ))它给我的错误

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



任何人可以帮助我解决这个问题?



 公共类验证
{
公共无效验证(字符串的xmlString)
{
布尔BRET = TRUE;

串xmlpath中= @C:\x.xml
串xsdPath = @C:\general.xsd

XmlReaderSettings设置=新XmlReaderSettings();
Settings.Schemas.Add(,xsdPath);
Settings.ValidationType = ValidationType.Schema;
Settings.ValidationEventHandler + =新的ValidationEventHandler(SettingsValidationEventHandler);

的XmlReader列表= XmlReader.Create(xmlpath中,设置);

// StringBuilder的输出=新的StringBuilder();
,而(list.Read())
{
}

//File.WriteAllText(@\"D:\Output.xml,output.ToString ());
}

静态无效SettingsValidationEventHandler(对象发件人,ValidationEventArgs E)
{
如果(e.Severity == XmlSeverityType.Warning)
{
MessageBox.Show(警告:);
MessageBox.Show(e.Message);
}
,否则如果(e.Severity == XmlSeverityType.Error)
{
MessageBox.Show(错误:);
MessageBox.Show(e.Message);
}
}

}

这是我的XML:

 < XML版本=1.0编码=UTF-16>? 
< FlashList的xmlns:XSI =http://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =http://www.w3.org/2001/XMLSchemaVIN = XXXXXXXXXXXXX>
<闪光ECUtype =XXX/>
< / FlashList>



这是我的XSD:

 <?XML版本=1.0编码=UTF-16>?; 
< XS:模式attributeFormDefault =不合格将elementFormDefault =合格的xmlns:XS =http://www.w3.org/2001/XMLSchema>
< XS:元素的名称=FlashList>
< XS:复杂类型>
< XS:序列>
< XS:元素的名称=闪光的maxOccurs =无界的minOccurs =0>
< XS:复杂类型>
< XS:simpleContent的>
< XS:扩展基板=XS:字符串>
< XS:属性类型=XS:字符串NAME =ECUtype使用=可选/>
< / XS:扩展>
< / XS:simpleContent的>
< / XS:复杂类型>
< / XS:组件>
< XS:元素的名称=错误的maxOccurs =无界的minOccurs =0>
< XS:复杂类型>
< XS:simpleContent的>
< XS:扩展基板=XS:字符串>
< XS:属性类型=XS:字节NAME =码使用=可选/>
< / XS:扩展>
< / XS:simpleContent的>
< / XS:复杂类型>
< / XS:组件>
< / XS:序列>
< XS:属性类型=XS:字符串NAME =VIN/>
< / XS:复杂类型>
< / XS:组件>
< / XS:架构>


解决方案

您文件的编码的现实似乎与发生冲突你的XML声明中指定。如果你的文件实际上是使用单字节字符,宣告编码=UTF-16不会改变它使用两个字节的字符,例如



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

 <?XML版本=1.0编码=UTF-16>?; 



 <?XML版本=1.0>?; 

您还可以将文件加载到一个字符串作为一个变通使用的loadXML()


I am writing a XML validator with XSD

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

There is no Unicode byte order mark. Cannot switch to 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);
            }
        }

    }

This is my 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>

And this is my 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>

解决方案

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.

Try removing the conflicting encoding from the XML declaration. Replace

<?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().

这篇关于没有Unicode字节顺序标记。无法切换到Unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆