Csharp/CSharp Tutorial/XML/Xml Validation — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 15:31, 26 мая 2010
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);
}
}