[英]Error when validating an XML file with XSD
我正在尝试使用XSD验证XML文件,但是对于每个元素和属性,我都会收到“ 找不到元素'xxx'的架构信息 ”。
我的C#代码是:
public ReadeXmlFile(string FilePath)
{
var settings = new XmlReaderSettings
{
IgnoreComments = true,
ValidationType = ValidationType.Schema,
CheckCharacters=true,
ValidationFlags= XmlSchemaValidationFlags.ReportValidationWarnings
};
settings.ValidationEventHandler += settings_ValidationEventHandler;
var xsdReader = new XmlTextReader("KeyEmFileSchema.xsd");
settings.Schemas.Add(null, xsdReader);
using (var reader = XmlTextReader.Create(FilePath, settings))
{
while (reader.Read()){}
}
}
void settings_ValidationEventHandler(object sender, ValidationEventArgs e)
{
Debug.WriteLine(e.Severity + " - " + e.Message);
}
我的XML文件:
<?xml version="1.0" encoding="utf-16"?>
<keyem description="test"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:noNamespaceSchemaLocation="http://tempuri.org/KeyEmFileSchema.xsd"
>
<layout type="keyboard" height="300" width="300">
<groupp text="rad 1">
<key color="Black" macro="{ESC}10{ESC}43{MOUSERESET}" text="Ordi-\nnarie"/>
<key color="Gray" macro="{ESC}10{ESC}B0{MOUSERESET}" text="Släck\nskärm"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2" icon="dfkhfkjsdhfkjdsf">
<shift color="Blue" macro="{ESC}1C{ESC}81{MOUSERESET}" text="Annan Skärm"/>
</key>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
<empty/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n4"/>
</groupp>
<group text="rad 2">
<key color="Black" macro="{ESC}10{ESC}43{MOUSERESET}" text="Ordi-\nnarie"/>
<key color="Gray" macro="{ESC}10{ESC}B0{MOUSERESET}" text="Släck\nskärm"/>
<group color ="Blue" text="test">
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
</group>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
<empty/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n4"/>
</group>
</layout>
</keyem>
我的XSD文件
<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="FileSchema"
targetNamespace="http://tempuri.org/KeyEmFileSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:mstns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:annotation>
<xs:documentation xml:lang="sv-se">Definition av layout</xs:documentation>
</xs:annotation>
<!--Definition av attribut-->
<xs:attribute name="description" type="xs:string"/>
<xs:attribute name="text" type="xs:string"/>
<xs:attribute name="height" type="xs:positiveInteger"/>
<xs:attribute name="width" type="xs:positiveInteger"/>
<xs:attribute name="type" type="LayoutTypeSet" default="keyboard"/>
<xs:attribute name="macro" type="xs:string"/>
<xs:attribute name="icon" type="xs:base64Binary"/>
<xs:attribute name="color" type="ColorType"/>
<!--Definition av attributgrupp-->
<xs:attributeGroup name="ShiftKeyAttributeGroup">
<xs:attribute ref="color" use="optional"/>
<xs:attribute ref="macro" use="optional"/>
<xs:attribute ref="text" use="required"/>
<xs:attribute ref="icon" use="optional"/>
</xs:attributeGroup>
<!--Definition av root-->
<xs:element name="keyem">
<xs:complexType>
<xs:sequence>
<xs:element name="layout" type="LayoutType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute ref="description" use="optional"/>
</xs:complexType>
</xs:element>
<!--Definition av komplexa typer-->
<xs:complexType name="GroupKeyType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="LayoutType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute ref="type" use="required"/>
<xs:attribute ref="height" use="optional"/>
<xs:attribute ref="width" use="optional"/>
</xs:complexType>
<xs:complexType name="GroupType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="empty" type="EmptyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute ref="text" use="required"/>
<xs:attribute ref="color" use="optional"/>
</xs:complexType>
<xs:complexType name="EmptyType">
<xs:sequence>
<xs:element name="empty"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ShiftType">
<xs:attributeGroup ref="ShiftKeyAttributeGroup"/>
</xs:complexType>
<xs:complexType name="KeyType">
<xs:sequence>
<xs:element name="shift" type="ShiftType" minOccurs ="0" maxOccurs="1"/>
</xs:sequence>
<xs:attributeGroup ref="ShiftKeyAttributeGroup"/>
</xs:complexType>
<!--Definition av enkla typer-->
<xs:simpleType name="ColorType">
<xs:restriction base="xs:string">
<!--Regex för att antingen matcha färg angivet på formen #rrggbb eller som är skriven i klarspråk, t.ex. "Green"-->
<xs:pattern value="\#[0-9a-fA-F]{6}|[a-zA-Z]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="LayoutTypeSet">
<xs:restriction base="xs:string">
<xs:enumeration value="keyboard"/>
<xs:enumeration value="list"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
根据您的模式,您在xml内有一个错误的节点:
<groupp text="rad 1">
可以在该级别插入组或关键元素。
关于您的特定问题,请尝试使用架构文件中的下一个根节点:
<xs:schema id="FileSchema"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
...
</xs:schema>
如果您在KeyEmFileSchema.xsd
文件中定义了架构,则可以使用schemaLocation
代替noNamespaceSchemaLocation
:
<keyem description="test"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
>
它定义了可以在其中找到并使用定义http://tempuri.org/KeyEmFileSchema.xsd
文件名(如果需要,可以包含路径)。 然后你可以使用
XmlReaderSettings settings = new XmlReaderSettings ();
settings.ValidationType = ValidationType.Schema;
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
settings.ValidationFlags = XmlSchemaValidationFlags.ProcessSchemaLocation;
XmlReader reader = XmlReader.Create (xmlFilePath, settings);
如果需要,可以另外使用ValidationEventHandler
。 没有处理程序,您将收到有关验证错误信息的异常。
更新 :在您的根元素中包含xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
和xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
之后xml文件,您已经在Visual Studio文本编辑器中看到了 XML文件中的错误:
keyem
元素中未声明的属性description
的keyem
。 layout
元素中所有未声明的属性的使用。 groupp
元素代替group
。 更新2 :要求编号3:将groupp
替换为group
很明确。 因为没有人评论我的回答,所以我想有人问“ XML文件的属性怎么了?”。 好吧,我不得不对此发表更多评论。
XSD文件中的问题是,您将属性声明为不是某些简单类型,属性组或某些元素的属性的一部分 。 您只需声明一些“独立”属性 ,然后在每个“ ref”引用中使用它们即可。 通常是可能的,但是这种方式需要使用限定属性 。 因此,如果您在架构中未进行任何更改,则将遵循XML文件的固定版本
<?xml version="1.0" encoding="utf-16"?>
<keyem xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"
a:description="test" xmlns:a="http://tempuri.org/KeyEmFileSchema.xsd"
>
<layout a:type="keyboard" a:height="300" a:width="300">
<group a:text="rad 1">
<key a:color="Black" a:macro="{ESC}10{ESC}43{MOUSERESET}" a:text="Ordi-\nnarie"/>
<key a:color="Gray" a:macro="{ESC}10{ESC}B0{MOUSERESET}" a:text="Släck\nskärm"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n1"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n2" a:icon="dfkhfkjsdhfkjdsf">
<shift a:color="Blue" a:macro="{ESC}1C{ESC}81{MOUSERESET}" a:text="Annan Skärm"/>
</key>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n3"/>
<empty><empty/></empty>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n4"/>
</group>
<group a:text="rad 2">
<key a:color="Black" a:macro="{ESC}10{ESC}43{MOUSERESET}" a:text="Ordi-\nnarie"/>
<key a:color="Gray" a:macro="{ESC}10{ESC}B0{MOUSERESET}" a:text="Släck\nskärm"/>
<group a:color ="Blue" a:text="test">
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n1"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n2"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n3"/>
</group>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n1"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n2"/>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n3"/>
<empty><empty/></empty>
<key a:color="Gray" a:macro="{ESC}1B{ESC}81{MOUSERESET}" a:text="Skärm\n4"/>
</group>
</layout>
</keyem>
简短说明 : <empty>
元素在您的模式中定义错误,因此要遵循该模式,我们必须像<empty><empty/></empty>
这样使用它。
可能的更改的另一个版本(您可能会更喜欢)是将所有属性放在elements或attribute group的定义中 。 在您的情况下使用简单类型似乎不需要我。 因此,该模式的固定版本可以如下:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="FileSchema"
targetNamespace="http://tempuri.org/KeyEmFileSchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:mstns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:annotation>
<xs:documentation xml:lang="sv-se">Definition av layout</xs:documentation>
</xs:annotation>
<!--Definition av attributgrupp-->
<xs:attributeGroup name="ShiftKeyAttributeGroup">
<xs:attribute name="color" type="ColorType"/>
<xs:attribute name="macro" type="xs:string"/>
<xs:attribute name="text" type="xs:string" use="required"/>
<xs:attribute name="icon" type="xs:base64Binary"/>
</xs:attributeGroup>
<!--Definition av root-->
<xs:element name="keyem">
<xs:complexType>
<xs:sequence>
<xs:element name="layout" type="LayoutType" minOccurs="0" maxOccurs="1"/>
</xs:sequence>
<xs:attribute name="description" type="xs:string"/>
</xs:complexType>
</xs:element>
<!--Definition av komplexa typer-->
<xs:complexType name="GroupKeyType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="LayoutType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="type" type="LayoutTypeSet" use="required"/>
<xs:attribute name="height" type="xs:positiveInteger"/>
<xs:attribute name="width" type="xs:positiveInteger"/>
</xs:complexType>
<xs:complexType name="GroupType">
<xs:choice maxOccurs="unbounded">
<xs:element name="group" type="GroupType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="key" type="KeyType" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="empty" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
<xs:attribute name="text" type="xs:string" use="required" />
<xs:attribute name="color" type="ColorType"/>
</xs:complexType>
<xs:complexType name="ShiftType">
<xs:attributeGroup ref="ShiftKeyAttributeGroup"/>
</xs:complexType>
<xs:complexType name="KeyType">
<xs:sequence>
<xs:element name="shift" type="ShiftType" minOccurs ="0" maxOccurs="1"/>
</xs:sequence>
<xs:attributeGroup ref="ShiftKeyAttributeGroup"/>
</xs:complexType>
<!--Definition av enkla typer-->
<xs:simpleType name="ColorType">
<xs:restriction base="xs:string">
<!--Regex för att antingen matcha färg angivet på formen #rrggbb eller som är skriven i klarspråk, t.ex. "Green"-->
<xs:pattern value="\#[0-9a-fA-F]{6}|[a-zA-Z]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="LayoutTypeSet">
<xs:restriction base="xs:string">
<xs:enumeration value="keyboard"/>
<xs:enumeration value="list"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
如果我们保存新的模式在文件KeyEmFileSchema1.xsd
然后KeyEmFileSchema.xml
文件几乎可以是因为你之前有相同的:
<?xml version="1.0" encoding="utf-16"?>
<keyem xmlns="http://tempuri.org/KeyEmFileSchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema1.xsd"
description="test"
>
<layout type="keyboard" height="300" width="300">
<group text="rad 1">
<key color="Black" macro="{ESC}10{ESC}43{MOUSERESET}" text="Ordi-\nnarie"/>
<key color="Gray" macro="{ESC}10{ESC}B0{MOUSERESET}" text="Släck\nskärm"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2" icon="dfkhfkjsdhfkjdsf">
<shift color="Blue" macro="{ESC}1C{ESC}81{MOUSERESET}" text="Annan Skärm"/>
</key>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
<empty/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n4"/>
</group>
<group text="rad 2">
<key color="Black" macro="{ESC}10{ESC}43{MOUSERESET}" text="Ordi-\nnarie"/>
<key color="Gray" macro="{ESC}10{ESC}B0{MOUSERESET}" text="Släck\nskärm"/>
<group color ="Blue" text="test">
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
</group>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n1"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n2"/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n3"/>
<empty/>
<key color="Gray" macro="{ESC}1B{ESC}81{MOUSERESET}" text="Skärm\n4"/>
</group>
</layout>
</keyem>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.