validation in .Net?
2002-08-05 17:54:25 Niel Bornstein
It's a fair question, and one which I did not address in this article.
Basically, the validation of an XML document with a particular schema is done with XmlValidatingReader. One approach might be to create an instance of the DogShow class, then use XmlSerializer to write it to a MemoryStream. Then create an XmlValidatingReader, set its ValidationType to ValidationType.Schema, add the schema to its XmlSchemaCollection, and call Read().
The following code snippet, replacing the XmlWriter portion of the C# sample, should provide some direction:
Stream stream = new MemoryStream();
TextWriter writer = new StreamWriter(stream);
serializer.Serialize(writer,show);
stream.Seek(0,SeekOrigin.Begin);
XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(stream));
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add("", "DogShow.xsd");
while (reader.Read()) {
// do nothing
}