Validate against Schema for XDocument in CSharp

Description

The following code shows how to validate against Schema for XDocument.

Example


    /*ww  w  . j  a v a 2 s  . com*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml.Schema;

public class MainClass
{
    public static void Main()
    {
        XDocument doc = XDocument.Load("xml file");
        XmlSchemaSet schema = new XmlSchemaSet();
        schema.Add(null, "schema file");
        ValidationEventHandler handler = new ValidationEventHandler(MyHandler);
        doc.Validate(schema, handler);
    }

    public static void MyHandler(object sender, ValidationEventArgs e)
    {
        Console.WriteLine(e.Message);
    }

}




















Home »
  C# Tutorial »
    XML »




Load Parse
Document
Element
Attribute
Namespace
Query
Save
Schema
Style