【问题标题】:XML schema validation results "Attribute not declared"XML 模式验证结果“未声明属性”
【发布时间】:2013-03-15 07:33:37
【问题描述】:

我根据我的 XML 模式验证 XML 文档。我最近在 XML 文件和 XSD 模式中添加了一个新属性。但是在进行验证时,我总是会遇到异常

基础{System.EventArgs}:{System.XML.Shema.ValidationEventArgs}
例外:“未声明 Anhaenger 属性。
消息:“未声明 Anhaenger 属性。

这里是我的 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>

这是 XML 文件中的属性:

 Anhaenger="ANH - 0815"

这是我用于在我的应用程序中进行更正的代码:

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;
}

那么,有人知道我做错了什么吗?非常感谢您的帮助

最好的问候 阿德莱诺

【问题讨论】:

    标签: c# xsd


    【解决方案1】:

    catch 位于 XSD 的目标命名空间中;我猜(您没有显示 xs:schema 属性集)因为您的 XML 属性被声明为全局属性,所以它必须以与您的架构的 targetNamespace 匹配的别名作为前缀。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多