使用XSD验证XML文件时出错

时间:2010-09-30 08:21:41

标签: c# xml xsd xsd-validation

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

2 个答案:

答案 0 :(得分:1)

根据您的架构,您的xml中有一个错误的节点:

<groupp text="rad 1">

只需在该级别插入组或键元素即可。

关于您的特定问题,请尝试使用架构文件中的下一个根节点:

<xs:schema id="FileSchema"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
...
</xs:schema>

答案 1 :(得分:1)

如果您在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。如果没有处理程序,您将收到有关验证错误信息的异常。

更新:在您的xml文件的根元素中包含xmlns="http://tempuri.org/KeyEmFileSchema.xsd"xsi:schemaLocation="http://tempuri.org/KeyEmFileSchema.xsd KeyEmFileSchema.xsd"之后,您将在Visual Studio文本编辑器中看到 XML文件中的错误:

  1. description元素中使用未声明的属性keyem
  2. 使用layout元素中尚未声明的所有属性。
  3. 使用groupp元素代替group
  4. 更新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>一样使用它。

    可能的更改的另一个版本(您可能更喜欢)是将所有属性放在元素或属性组的定义中。在你的情况下使用简单类型似乎我不需要。因此,架构的固定版本可以遵循:

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