保存する前に、コードで作成しているXMLドキュメントを検証しようとしています。ただし、意図的に間違った値を入力した場合でも、コードは常に問題なく検証を通過します。コードの問題は何ですか?
private XmlDocument xmlDocChanges = new XmlDocument();
public void Validate()
{
xmlDocChanges.Schemas.Add("http://www.w3.org/2001/XMLSchema", "xsd/Customization.xsd");
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationCallBack);
xmlDocChanges.Validate(eventHandler);
}
public void ValidationCallBack (object sender, ValidationEventArgs args)
{
if(args.Severity == XmlSeverityType.Error || args.Severity == XmlSeverityType.Warning)
{
throw new Exception(args.Exception.Message);
}
}
例XSDを編集します。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema
attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="FirstNode">
<xs:annotation>
<xs:documentation>First node</xs:documentation>
</xs:annotation>
<xs:attribute name="Identifier" type="xs:string" use="required" />
<xs:attribute name="Bool" type="xs:boolean" use="optional" />
</xs:complexType>
</xs:schema>
XML
<Customizations FormatVersion="1" xsi:noNamespaceSchemaLocation="Customization.xsd">
<Customization>
<Application name="App">
<FirstNode Identifier="one" Bool="NoValue"></FirstNode>
</Application>
</Customization>
</Customizations>