سؤال

أحاول التحقق من صحة ملف XML مع XSD ، لكنني أحصل على "لا يمكن العثور على معلومات المخطط لعنصر "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>
هل كانت مفيدة؟

المحلول

إذا حددت المخطط في 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 الخاص بك:

  1. استخدام السمة غير المعلنة description في keyem عنصر.
  2. استخدام جميع السمات في layout عنصر غير معلن أيضًا.
  3. استخدام groupp عنصر بدلا من group.

تحديث 2: المتطلبات رقم 3: لاستبدال groupp ل group واضح. لأن أحد علق إجابتي ، أفترض أن شخصًا ما يسأل "ما هو الخطأ في سمات ملف XML؟". حسنًا ، لا بد لي من التعليق على هذا أكثر.

المشكلة في ملف XSD الخاص بك هي أنك تعلن سماتها ليس جزءًا من بعض الأنواع البسيطة أو مجموعة السمات أو سمات بعض العناصر. أنت فقط تعلن بعض سمات "مستقلة" ثم استخدم هناك لكل مرجع "المرجع". إنه ممكن بشكل عام ، لكن بهذه الطريقة تتطلب استخدامها تأهلت صفات. لذلك إذا لم تقم بأي تغييرات في مخططك ، فسيتابع الإصدار الثابت من ملف 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>

نصائح أخرى

وفقًا لمخططك ، لديك عقدة خاطئة داخل XML الخاص بك:

<groupp text="rad 1">

مجرد مجموعة أو عنصر رئيسي يمكن إدراجها على هذا المستوى.

فيما يتعلق بمشكلتك المحددة ، حاول استخدام عقدة الجذر التالية داخل ملف المخطط الخاص بك:

<xs:schema id="FileSchema"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
...
</xs:schema>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top