I'm using XmlSerializer to deserialize Xml achives. But I found the class xsd.exe generated only offers capability to read the xml, but no validation. For example, if one node is missing in a document, the attribute field of the generated class will be null, rather than throws a validation exception as I expected. How can I achieve that? Thanks!
The following code should validate against a schema while deserializing. Similar code can be used to validate against a schema while serializing.
private static Response DeserializeAndValidate(string tempFileName)
{
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(LoadSchema());
Exception firstException = null;
var settings = new XmlReaderSettings
{
Schemas = schemas,
ValidationType = ValidationType.Schema,
ValidationFlags =
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ReportValidationWarnings
};
settings.ValidationEventHandler +=
delegate(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
{
Console.WriteLine(args.Message);
}
else
{
if (firstException == null)
{
firstException = args.Exception;
}
Console.WriteLine(args.Exception.ToString());
}
};
Response result;
using (var input = new StreamReader(tempFileName))
{
using (XmlReader reader = XmlReader.Create(input, settings))
{
XmlSerializer ser = new XmlSerializer(typeof (Response));
result = (Response) ser.Deserialize(reader);
}
}
if (firstException != null)
{
throw firstException;
}
return result;
}