没有 Unicode 字节顺序标记.无法切换到 Unicode
- 作者: 丿樱花灬
- 来源: 51数据库
- 2023-02-13
问题描述
我正在用 XSD 编写一个 XML 验证器.
I am writing an XML validator with XSD.
下面是我所做的,但是当验证器到达 while (list.Read()) 行时,它给了我错误
Below is what I did, but when the validator reached the line while (list.Read()) it gives me the error
没有Unicode字节顺序标记.无法切换到 Unicode.
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.51sjk.com/Upload/Articles/1/0/343/343155_20230213091630917.jpg" xmlns:xsd="http://www.51sjk.com/Upload/Articles/1/0/343/343155_20230213091653293.jpg" vin="xxxxxxxxxxxxx"> <flash ECUtype="xxx" /> </FlashList>
XSD
<?xml version="1.0" encoding="utf-16"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.51sjk.com/Upload/Articles/1/0/343/343155_20230213091653293.jpg"> <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" 不会将其更改为使用两字节字符.
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 声明中删除冲突的编码.替换
Try removing the conflicting encoding from the XML declaration. Replace
<?xml version="1.0" encoding="utf-16"?>
与
<?xml version="1.0"?>
您也可以使用 LoadXML() 将文件加载到字符串中作为解决方法.
You may also be able to load the file into a string as a work-around using LoadXML().
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源