Reputation: 5725
Is it possible to use the XML Schema validation and XMLSerializer together?
My project currently uses XMLSerializer. To validate the schema, we are programatically checking the values like:
if(String.IsNullOrEmpty(person.Name))
throw new Exception();
Thanks!
Upvotes: 0
Views: 3572
Reputation: 574
You can add some additional checking to the xsd, by using the element restriction element. Each type have some facets that you can apply to the type/element - ranging from simple min/max length to regular expressions.
You can even take it one step further and use the appinfo xsd element, where you can add custom specification for etc. validation checking. This step however require you to parse each xml node individually, as the normal Xml Schema Validation don't trigger appinfo functionality.
As a last resort you can even have a look at Schematron, which is a formalized way to add quite complex validation to your xsd, but it is in itself a bit complex, and in many situations overkill.
Upvotes: 2
Reputation: 8581
Actually you can just provide an XSD within the XML. When you read it using the XMLSerializer, it will throw exceptions if the XML is not matching the XSD.
Upvotes: 0
Reputation: 2763
I would suggest using the XmlValidatingReader for validatiing the schema. Please see http://www.codeproject.com/KB/XML/Serialization.aspx for an example...
Upvotes: 1