XSD架构验证似乎忽略了必需的属性

时间:2017-12-04 18:59:09

标签: c# xml

我使用XmlReader根据我的XSD架构文件验证我的XML文件,并且它有一个例外:它没有报告缺少必需的属性。

然而,它会报告不符合XSD的属性(名称拼写错误,不允许内容)

例如,即使两个属性都需要,这两个都会在没有警告的情况下进行验证:

<Margin Units="IN">
<Margin Units="IN" Center="true">

然而,这些将发出警告(其中Units是Enum,Center是布尔值):

<Margin Units="IN" Center="123">
<Margin Units="abc" Center="123">

为了让事情更加混乱(对我而言),使用Notepad ++插件&#34; XML工具&#34;,相同的文件和条件将无法验证,并且将无法使用XMLSpy。

似乎只有C#/ .Net不会发出警告。

这是我的验证码:

public string ValidateXML(string sXmlString)
{
    validationErr = "";
    try
    {
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.CloseInput = true;
        settings.ValidationEventHandler += ValidationHandler;
        settings.ValidationType = ValidationType.Schema;
        settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings |
                                   XmlSchemaValidationFlags.ProcessIdentityConstraints |
                                   XmlSchemaValidationFlags.ProcessSchemaLocation;

        StringReader r = new StringReader(sXmlString);
        XmlReader validatingReader = XmlReader.Create(r, settings);
        XmlDoc = new XmlDocument();
        XmlDoc.Load(validatingReader); //errors are put into the validationErr var in the ValidationHandler
    }
    catch (Exception exc)
    {
        validationErr = "XML EXCEPTION: " + exc.Message + Environment.NewLine + validationErr;
    }
    return validationErr;
}

private static void ValidationHandler(object sender, ValidationEventArgs e)
{
    if (e.Severity == XmlSeverityType.Error || e.Severity == XmlSeverityType.Warning)
    {
        validationErr += "XML Parse Error Line: " +
                    e.Exception.LineNumber + " Position: " +
                    e.Exception.LinePosition + " Message: " +
                    e.Exception.Message + Environment.NewLine;
    }
}

这是我的XSD:

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="SampleSchema">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Margins" minOccurs="0"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Margins">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Header" minOccurs="0"/>
                <xs:element ref="Footer" minOccurs="0"/>
                <xs:element ref="LeftSide" minOccurs="0"/>
                <xs:element ref="RightSide" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute ref="Units" use="required"/>
            <xs:attribute ref="CenterHorizontal" use="required"/>
            <xs:attribute ref="CenterVertical" use="required"/>
            <xs:attribute ref="PriorityHorizontal" use="required"/>
            <xs:attribute ref="PriorityVertical" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Header">
        <xs:simpleType>
            <xs:restriction base="xs:decimal">
                <xs:minInclusive value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="Footer">
        <xs:simpleType>
            <xs:restriction base="xs:decimal">
                <xs:minInclusive value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="LeftSide">
        <xs:simpleType>
            <xs:restriction base="xs:decimal">
                <xs:minInclusive value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:element name="RightSide">
        <xs:simpleType>
            <xs:restriction base="xs:decimal">
                <xs:minInclusive value="0"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
    <xs:attribute name="Units" default="PT">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="IN"/>
                <xs:enumeration value="MM"/>
                <xs:enumeration value="PT"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="CenterHorizontal" type="xs:boolean" default="true">
    </xs:attribute>
    <xs:attribute name="CenterVertical" type="xs:boolean" default="true">
    </xs:attribute>
    <xs:attribute name="PriorityHorizontal" default="Left">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="Left"/>
                <xs:enumeration value="Right"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
    <xs:attribute name="PriorityVertical" default="Header">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="Header"/>
                <xs:enumeration value="Footer"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:attribute>
</xs:schema>

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<SampleSchema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="sample.xsd">
    <Margins Units="PT" CenterHorizontal="false" CenterVertical="false" PriorityVertical="Header">
        <Header>36</Header>
        <Footer>36</Footer>
        <LeftSide>36</LeftSide>
        <RightSide>36</RightSide>
    </Margins>
</SampleSchema>

初始验证成功后,我是否必须编写代码以显式检查XML文件中的属性?

或者有没有办法让XmlReader验证抛出这些警告?

1 个答案:

答案 0 :(得分:1)

尝试删除属性定义中的“default”。 MSDN says

  

...架构的处理器应该像使用默认值指定属性一样,如果它实际上不在实例文档中。

看起来这就是“必需”验证不起作用的原因。