Reputation: 176
I generated C# classes based on XSD using the xsd.exe tool from the SDK. Then I can use that class to [de]serialize objects using XmlSerializer... However the serializer seems to be very forgiving.
Is it possible that I can make the serializer throw an exception in case there is missing property or a "strange" XML node?
I think one way is to modify the setter of the property and make it validate the data (or use XSD validation)... However is there any other alternative solution for this problem ?
Upvotes: 0
Views: 128
Reputation: 74530
You can implement the IXmlSerializable
interface and in the ReadXml
method implementation, check for the specific elements that you require, throwing exceptions when you don't find them (or setting whatever notification you need to).
If you want to use a schema for validation (to use the minOccurs
and maxOccurs
schema attributes, for example), then you can configure the XmlReader
instance to validate against the schema by setting the Schemas
property on the XmlReaderSettings
class that you pass to the Create
method (note there are overloads of Create
which take a TextReader
, etc.).
Upvotes: 2