tags:

views:

87

answers:

1

I'm trying to figure out how to correct his deprecated xml schema validation code.

public static bool ValidateXml(string xmlFilename, string schemaFilename)
{
    ⁞

    //Forward stream reading access to data
    XmlTextReader forwardStream = new XmlTextReader(xmlFilename);

    //deprecated way of checking agaisnt a schema -- update.
    //xmlreader class.
    XmlValidatingReader validation = new XmlValidatingReader(forwardStream);
    validation.ValidationType = ValidationType.Schema;

    //XmlReader validator = new XmlReader.Create(

    XmlSchemaCollection schemas = new XmlSchemaCollection();
    schemas.Add(null, schemaFilename);
    validation.Schemas.Add(schemas);

    ⁞
+5  A: 

you need to use XmlReader and XmlReaderSettings instead of depricated classes. Below is an example:

// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd");

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

// Parse the file. 
while (reader.Read());

more details here: Validating XML Data with XmlReader

regards

serge_gubenko