代码之家  ›  专栏  ›  技术社区  ›  IlliakaillI

带有xsd:any的架构-XmlReader验证错误

  •  2
  • IlliakaillI  · 技术社区  · 15 年前

    尝试使用xsd:any元素时,我的模式中的类型有问题。 在验证期间,我遇到验证异常:“MerchanceAccount”元素未声明。

    其思想是能够在extendedproperties元素中指定任何属性和值。 请告诉我做错了什么。

    架构的一部分

    ...
    <xsd:complexType name="ExtendedPropertiesType">
        <xsd:sequence>
          <xsd:any minOccurs="0" maxOccurs="unbounded" />
        </xsd:sequence>
    </xsd:complexType>
    
    <xsd:complexType name="ProcessorInstanceType">
      <xsd:all>
        <xsd:element name="Id" type="xsd:string" />
        <xsd:element name="Descriptor" type="xsd:string" />
        <xsd:element minOccurs="0" name="ExtendedProperties" type="ExtendedPropertiesType" />
      </xsd:all>
      <xsd:attribute name="name" type="xsd:string" />
    </xsd:complexType>
    ...
    

    XML文件的一部分:

    ...
    <ProcessorInstance name="aaaa">
      <Id>37fc527b-2845-43d0-99ca-ac1ff6f0ed86</Id>
      <Descriptor>Test</Descriptor>
    
      <ExtendedProperties>
        <MerchantAccount>1111</MerchantAccount>
      </ExtendedProperties>
    </ProcessorInstance>
    ...
    

    验证代码:

    private static XmlDocument loadConfigurationXml(FileInfo configFile)
        {
            //load schema
            var sr = new StringReader(Schemas.ConfigurationSchema);
            var schema = XmlSchema.Read(sr, (o, ea) => { throw ea.Exception; });
            //validate against the schema
            var schemas = new XmlSchemaSet();
            schemas.Add(schema);
            var readerSettings = new XmlReaderSettings
            {
                ValidationType = ValidationType.Schema, 
                ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings,
                Schemas = schemas,
            };
            readerSettings.ValidationEventHandler += (o, ea)=>
            {
                throw new PaynetValidationException(
                    string.Format("Invalid configuration file, see schema for details: {0}", 
                                  ea.Message), 
                    ea.Exception);
            };
            var reader = XmlReader.Create(configFile.FullName, readerSettings);
            //parse and validate config file
            while (reader.Read()){}
    
            var ret = new XmlDocument();
            if (configFile.Length != 0)
                ret.Load(configFile.FullName);
    
            return ret;
        }
    
    1 回复  |  直到 15 年前
        1
  •  4
  •   John Saunders    15 年前

    这是因为 processContents 属性是 strict . 如果希望在没有元素的架构时验证,请使用

    <xs:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
    

    顺便说一句,如果你正在设计这个模式,我建议你远离 xs:all . 允许以任何顺序输入元素听起来可能是个好主意,但这可能导致内容模型不明确,并且会导致代码处理模式的彻底崩溃。