Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Using ValidationEventHandler
using System;
using System.IO;
using System.Xml.Schema;
public class ValidateSchema {
public static void Main(string [] args) {
ValidationEventHandler handler = new ValidationEventHandler(ValidateSchema.Handler);
XmlSchema schema = XmlSchema.Read(File.OpenRead(args[0]),handler);
schema.rupile(handler);
}
public static void Handler(object sender, ValidationEventArgs e) {
Console.WriteLine(e.Message);
}
}
Validate Xml
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);
}
}