Validate Xml : Xml Validation « XML « C# / CSharp Tutorial






using System;
using System.Xml;
using System.Xml.Schema;

public class MainClass
{
  [STAThread]
  private static void Main()
  {
      string xmlFilename = "test.xml";
      string schemaFilename = "schema.xml";
        
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;

        XmlSchemaSet schemas = new XmlSchemaSet();
        settings.Schemas = schemas;

        schemas.Add(null, schemaFilename);
        
        settings.ValidationEventHandler += ValidationEventHandler;
        
    XmlReader validator = XmlReader.Create(xmlFilename, settings);                
    
    try
    {
      while (validator.Read()){}
    } catch (XmlException err) {
      Console.WriteLine(err.Message);
    } finally {
      validator.Close();
    }
  }
  private static void ValidationEventHandler(object sender, ValidationEventArgs args)
  {
    Console.WriteLine("Validation error: " + args.Message);
  }
}








30.19.Xml Validation
30.19.1.Validate Xml
30.19.2.Using ValidationEventHandler
30.19.3.Check for different Xml validation error level