The classes which are generated from an XML schema with xsd.exe do not implement every restriction of the original schema. No exceptions or warnings will be given if an XML document is serialized but the serialized document does not conform to the original schema.
This is especially common when using XML Schema restriction elements on simple types, as the generated C# classes will not enforce the same restrictions. For example, consider the schema fragment:
<xs:simpleType name="positiveLong">
<xs:restriction base="xs:long">
<xs:minExclusive value="0" />
</xs:restriction>
</xs:simpleType>
Any schema elements which use this type will result, when compiled using xsd.exe, in an object which uses the standard C# long type, which will happily accept negative numbers. Furthermore, the Serialize() method will quietly write out the negative number.
Therefore, it is recommended that any document generated using XmlSerializer should be revalidated against the original XML schema.
Recent Comments