正确验证XML文档XSD [英] Validating XML documents with XSD correctly
问题描述
作为一个很好的协议的XML消费和生产经验的开发者,我从来没有真正以前与互动模式。这是第一次,这是实际发生的我。
As a developer with a good deal of XML consuming and producing experience, I've never really interacted with schemas before. For the first time this is actually occurring for me.
我已经跨越了特色,我认为更多的是有据可查的错误运行。
I've run across a "feature" that I consider more of a bug which is well documented.
当使用XDocument.Validate()似乎有这样的情况,其中该文件将是有效的,如果它不指定的模式相匹配。我觉得这是最有可能在我的XSD,XML命名空间,并预计验证过程之间的关系的理解存在缺陷。
When using XDocument.Validate() it seems that there are cases in which the document will be valid if it doesn't match the schema specified. I feel this is most likely a flaw in my understanding of the relationship between XSDs, XML namespaces, and expected validation processes.
因此,我向你我的XML示例,我XSD的样品,和我的验证code。
Therefore I submit to you my XML sample, my XSD sample, and my validation code.
XML - 这是故意的错误文件
XML - this is INTENTIONALLY the wrong document.
<?xml version="1.0" encoding="utf-8" ?>
<SuppliesDefinitions
xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Supplies.xsd">
<Supply type="Common">
<Information/>
<Ritual/>
<Weapon/>
<Tool count="1"/>
<Tool count="2"/>
<Tool count="3"/>
</Supply>
<Supply type="Uncommon">
<Information/>
<Weapon/>
<Tool count="1"/>
<Tool count="2"/>
<Tool count="3"/>
<Tool count="4"/>
</Supply>
<Supply type="Rare">
<Information/>
<Rune/>
<Weapon/>
<Tool count="2"/>
<Tool count="3"/>
<Tool count="4"/>
</Supply>
</SuppliesDefinitions>
的XSD来验证它。 (再次,这是故意对以上XML错文件)
The XSD used to validate it. (Again, this is intentionally the WRONG document for the above XML)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Encounters"
targetNamespace="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
elementFormDefault="qualified"
xmlns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
xmlns:mstns="http://lavendersoftware.org/schemas/SteamGame/Data/Xml/Encounters.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:complexType name="ToolType">
<xs:attribute name="count" use="required" type="xs:int"/>
</xs:complexType>
<xs:complexType name="TaskType">
<xs:choice maxOccurs="unbounded" minOccurs="1">
<xs:element name="Weapon"/>
<xs:element name="Information"/>
<xs:element name="Tool" type="ToolType"/>
<xs:element name="Ritual"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="EncounterType">
<xs:sequence maxOccurs="unbounded" minOccurs="1">
<xs:element name="Task" type="TaskType"/>
</xs:sequence>
<xs:attribute name="name" use="required" type="xs:string"/>
</xs:complexType>
<xs:element name="EncounterDefinitions">
<xs:complexType>
<xs:sequence maxOccurs="unbounded" minOccurs="1">
<xs:element name="Encounter" type="EncounterType"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
和最后审定code。
private static void ValidateDocument(XDocument doc)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, XmlReader.Create(new StreamReader(XmlSchemaProvider.GetSchemaStream("Encounters.xsd"))));
doc.Validate(schemas, (o, e) =>
{
//This is never hit!
Console.WriteLine("{0}", e.Message);
Assert.False(e.Severity == XmlSeverityType.Error);
});
}
我想知道是否有人能解释一下我做错了。我觉得我做了一些不正确的假设,这应该是工作的方式。它使用一个XSD对一个完全不相关的XML文档将是无效的,似乎我。
I was wondering if someone can explain what I am doing wrong. I feel I'm making some incorrect assumptions about the way this SHOULD be working. It seems to me using one xsd against a completely unrelated XML document would be invalid.
推荐答案
有一个在你的XML没有节点可通过架构进行验证(命名空间是不同的)。作为结果,它不会报告任何错误。据我知道,对于那些不匹配任何模式节点的行为是让任何东西。
There is no nodes in your XML that can be validated by the schema (namespaces are different). As result it does not report any errors. As far as I know behavior for nodes that are not matched to any schema is allow anything.
您也可以将验证选项XmlReaderSettings ,以允许警告:
You also could set validation options in XmlReaderSettings to allow warnings:
ReportValidationWarnings - 表示事件应该如果验证警告发生时的报道。通常发出警告时,有没有DTD或XML Schema来验证一个特定的元素和属性对。该的ValidationEventHandler用于通知。
ReportValidationWarnings - Indicates that events should be reported if a validation warning occurs. A warning is typically issued when there is no DTD or XML Schema to validate a particular element or attribute against. The ValidationEventHandler is used for notification.
查看 XmlSchemaSet.Add 和的 HOW TO:通过使用多个架构验证XML文档,如果你希望从多个命名空间节点是present在XML 的
这篇关于正确验证XML文档XSD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!