开发者

Deprecated XML validation code problem C#

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.
    XmlV开发者_如何学CalidatingReader 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);

    ⁞


you need to use XmlReader and XmlReaderSettings instead of deprecated 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜