【问题标题】:Xml Xsd Validation Fails (xs:anyType)Xml Xsd 验证失败 (xs:anyType)
【发布时间】:2012-05-14 16:39:46
【问题描述】:

我有这个 XML 文件

<bookstore>  
  <test>
    <test2/>
  </test>
</bookstore>

还有这个 XSD 架构

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="bookstore" type="bookstoreType"/>     
  <xsd:complexType name="bookstoreType">
    <xsd:sequence maxOccurs="unbounded">  
      <xsd:element name="test" type="xsd:anyType" />
    </xsd:sequence>                                       
  </xsd:complexType>
</xsd:schema>

我打算从 C# 代码验证 xml 文件。 有一种验证 XML 文件的方法:

    // validate xml
    private void ValidateXml()
    {
        _isValid = true;

        // Get namespace from xml file
        var defaultNamespace = XDocument.Load(XmlFileName).Root.GetDefaultNamespace().NamespaceName;

        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
        settings.Schemas.Add(defaultNamespace, XsdFileName);
        settings.ValidationEventHandler += OnValidationEventHandler;

        // Create the XmlReader object.
        using(XmlReader reader = XmlReader.Create(XmlFileName, settings))
        {
            // Parse the file. 
            while (reader.Read()) ;    
        }
    }

    private void OnValidationEventHandler(object s, ValidationEventArgs e)
    {
        if (_isValid) _isValid = false;

        if (e.Severity == XmlSeverityType.Warning)
            MessageBox.Show("Warning: " + e.Message);
        else
            MessageBox.Show("Validation Error: " + e.Message);
    }

我知道,这个 XML 文件是有效的。但是我的代码重新出现了这个错误:

Validation Error: Could not find schema information for the element 'test2'

我的错误在哪里?

谢谢!!!

【问题讨论】:

标签: c# .net xml xsd xml-validation


【解决方案1】:

更新:我假设您的代码与您列出的错误相匹配(我已在 .NET 3.5SP1 上尝试过您的代码,但无法重现您的行为)。下面的解决方法应该肯定有效(您得到的错误与进程内容子句strict 一致,而不是lax)。

用允许 xsd:any 的复杂内容替换 &lt;xsd:element name="test" type="xsd:anyType" /&gt;,如下所示:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="bookstore" type="bookstoreType"/> 
    <xsd:complexType name="bookstoreType"> 
        <xsd:sequence maxOccurs="unbounded"> 
            <xsd:element name="test">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence> 
    </xsd:complexType> 
</xsd:schema> 

拥有“lax”仍然会产生一条消息;如果您希望该消息消失,您可以使用“跳过”。无论如何,xsd:any 中的 skiplax 都能满足您的需求。

【讨论】:

  • 你接受了这个答案,我有点惊讶。这可能是一种解决方法,但它不能解释为什么原来的失败。因此,我不赞成。正如您正确指出的那样,您的 XML 实例对您的 XSD 模式有效,我看不出它为什么会失败。
  • @MichaelKay,我同意答案没有明确说明一件事:我已经为可能是错误或编码错误的问题提供了解决方法。
猜你喜欢
  • 2019-06-23
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 2015-02-16
  • 1970-01-01
  • 2012-10-16
相关资源
最近更新 更多