我正在尝试使用 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>