I’m trying to validate a XML document I’m creating in the code before i save it. However my code always pass through the validation with no problem even when i input incorrect value on purpose. What is the problem with the code?
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);
}
}
EDIT
Example 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>
I found a solution. I had to send a empty namespace parameter in the add method.