Schema Validation : XmlSchema « XML « C# / CSharp Tutorial






using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;


    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument items = new XmlDocument();
            items.Load("items.xml");
            XmlSchema schema = XmlSchema.Read(new FileStream("items.xsd",FileMode.Open), new ValidationEventHandler(OnSchemaValidate));
            items.Schemas.Add(schema);
            items.Validate(new ValidationEventHandler(OnValidate));
            
        }

        static void OnValidate(object sender, ValidationEventArgs vargs)
        {
            Console.WriteLine(vargs.Message);
        }

        static void OnSchemaValidate(object sender, ValidationEventArgs vargs)
        {
            Console.WriteLine(vargs.Message);
        }
    }








30.20.XmlSchema
30.20.1.Create Xml schema
30.20.2.Register Schema validation event
30.20.3.Schema Validation