简体   繁体   English

XML模式验证结果“未声明属性”

[英]XML schema validation results “Attribute not declared”

I validate an XML document against my XML schema. 我根据我的XML模式验证了XML文档。 I recently added a new attribute in the XML files and the XSD schema. 我最近在XML文件和XSD架构中添加了一个新属性。 But when doing the validation I always get an exception 但是在进行验证时,我总是会遇到异常

base{System.EventArgs}: {System.XML.Shema.ValidationEventArgs} base {System.EventArgs}:{System.XML.Shema.ValidationEventArgs}
Exception: "The Anhaenger-Attribute is not declared. 例外:“未声明Anhaenger属性。
Message: "The Anhaenger-Attribute is not declared. 消息:“未声明Anhaenger属性。

Here the declaration in my XSD schema: 这是我的XSD模式中的声明:

<xs:attribute id="Anhaenger" name="Anhaenger" use="optional">
   <xs:simpleType>
      <xs:restriction base="D2TAnhaenger_Type" />
   </xs:simpleType>
</xs:attribute>
<xs:simpleType id="D2TAnhaenger_Type" name="D2TAnhaenger_Type">
   <xs:restriction base="xs:string">
      <xs:maxLength value="70" />
      <xs:whiteSpace value="collapse" />
   </xs:restriction>
</xs:simpleType>

and this is the attribute in the XML file: 这是XML文件中的属性:

 Anhaenger="ANH - 0815"

Here the code I use for doing the correction in my application: 这是我用于在应用程序中进行更正的代码:

protected XmlDocument ValidateAndCorrect(Stream XMLStream)
{
    // Verwenden von XMLReader, damit die Schemainformationen an den Knoten angefügt werden.
    // Dann können wir später korrigieren.
    XmlReaderSettings settings = new XmlReaderSettings();
    settings.Schemas.Add("", m_SchemaFile);
    settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
    settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
    settings.ValidationType = ValidationType.Schema;

    // Dokument aus dem Stream holen.
    XmlDocument document = new XmlDocument();
    XMLStream.Seek(0, SeekOrigin.Begin);
    XmlReader reader = XmlReader.Create(XMLStream, settings);
    document.Load(reader);

    int i = 0;

    bool validate = true;
    while (validate)
    {
        i++;
        Debug.WriteLine("--- Validierung und Korrektur beginnt ---");
        Debug.WriteLine("Durchlauf Nummer: " + i.ToString());

        m_errors = new List<ValidationEventArgs>();
        document.Validate(this.ValidationEventHandler);
        validate = CorrectValidationResult(document.ChildNodes);
    }

    return document;
}

So, does anyone have an idea what I do wrong? 那么,有人知道我做错了什么吗? Many thanks for any help 非常感谢您的帮助

Best regards Adleano 最好的问候Adleano

The catch is in the target namespace of your XSD; 捕获位于XSD的目标名称空间中。 I guess (you're not showing the xs:schema attribute set) that since your XML attribute is declared global, it must be prefixed with an alias which matches your schema's targetNamespace. 我猜(您没有显示xs:schema属性集),由于XML属性被声明为全局属性,因此必须在前缀之前添加一个与架构的targetNamespace相匹配的别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM