我想在我保存之前驗證在代碼中創建的XML文檔。但是,即使我輸入了不正確的值,我的代碼始終通過驗證,沒有問題。代碼有什麼問題?爲什麼XmlDocument.validate錯誤地驗證了具有空名稱空間的無效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>
這些方法是否被執行過?如果這樣的話提供你驗證的schema和xml。 – Rafal 2012-07-19 09:00:49
您可以爲使用中的XSD和XML文件添加示例嗎? – AksharRoop 2012-07-19 09:01:22
正在執行驗證,並且只有在驗證出現問題時才執行ValidationCallBack。 – Frozendragon 2012-07-19 09:18:22