XmlSchemaObject represents the root class for the Xml schema : Schema Validating « XML LINQ « C# / C Sharp






XmlSchemaObject represents the root class for the Xml schema

 

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
using System.Reflection;

public class ValidXSD
{
    public static int Main()
    {
        string xsd = "example.xsd";
        FileStream fs;
        XmlSchema schema;
            fs = new FileStream(xsd, FileMode.Open);
            schema = XmlSchema.Read(fs, new ValidationEventHandler(ShowCompileError));

            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.ValidationEventHandler += new ValidationEventHandler(ShowCompileError);
            schemaSet.Add(schema);
            schemaSet.Compile();

            XmlSchema compiledSchema = null;

            foreach (XmlSchema schema1 in schemaSet.Schemas())
            {
                compiledSchema = schema1;
            }

            schema = compiledSchema;

            if (schema.IsCompiled)
            {
                DisplayObjects(schema);
            }
            return 0;
    }

    private static void DisplayObjects(object o)
    {
        foreach (PropertyInfo property in o.GetType().GetProperties())
        {
            if (property.PropertyType.FullName == "System.Xml.Schema.XmlSchemaObjectCollection")
            {

                XmlSchemaObjectCollection childObjectCollection = (XmlSchemaObjectCollection)property.GetValue(o, null);

                foreach (XmlSchemaObject schemaObject in childObjectCollection)
                {
                    DisplayObjects(schemaObject);
                }
            }
        }
    }
    private static void ShowCompileError(object sender, ValidationEventArgs e)
    {
        Console.WriteLine("Validation Error: {0}", e.Message);
    }
}

   
  








Related examples in the same category

1.Successfully Validating an XML Element
2.Creating an XSD Schema by Inferring Itfrom an XML Document
3.Validating an XML Document with Default Validation Event Handling
4.Validating an XML Document with a Lambda Expression
5.Validating an XML Document