System.Xml

.NET Framework Documentation

XmlSchemaCollection Class

Represents a collection of XML Schema Definition Language (XSD) schemas.

Introduction

The XmlSchemaCollection class provides a way to manage multiple XML schemas within a single collection. This is useful when dealing with complex XML documents that rely on several different schema definitions. It allows for efficient retrieval and management of these schemas, ensuring proper validation and processing of XML data.

Key Features

  • Stores and organizes multiple XmlSchema objects.
  • Supports adding, removing, and accessing schemas by namespace URI.
  • Provides methods for validating XML documents against the schemas in the collection.
  • Facilitates schema resolution and import handling.

Syntax

public class XmlSchemaCollection : CollectionBase

Inheritance

ObjectCollectionBaseXmlSchemaCollection

Constructors

Constructor Description
XmlSchemaCollection() Initializes a new instance of the XmlSchemaCollection class.

Methods

Method Description
Add(XmlSchema schema) Adds an XmlSchema object to the collection.
Add(string namespace, string schemaLocation) Adds an XmlSchema object from the specified location to the collection, associated with the given namespace.
Remove(XmlSchema schema) Removes the specified XmlSchema object from the collection.
Item(string namespace) Gets the XmlSchema object associated with the specified namespace.
Contains(string namespace) Determines whether the collection contains an XmlSchema object for the specified namespace.

Example Usage

The following example demonstrates how to create an XmlSchemaCollection, add schemas to it, and then use it to validate an XML document.

using System; using System.Xml; using System.Xml.Schema; public class Example { public static void Main(string[] args) { // Create a schema collection XmlSchemaCollection schemaCollection = new XmlSchemaCollection(); // Add a schema (replace with actual file path or URL) try { schemaCollection.Add("http://www.example.com/myschema", "path/to/your/schema.xsd"); } catch (XmlSchemaException ex) { Console.WriteLine("Error loading schema: " + ex.Message); return; } // Create an XmlSchemaSet for validation XmlSchemaSet schemaSet = new XmlSchemaSet(); foreach (XmlSchema schema in schemaCollection) { schemaSet.Add(schema); } // Create an XmlReader to read the XML document XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.Schemas = schemaSet; readerSettings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; readerSettings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidity; // Load and validate the XML document try { using (XmlReader reader = XmlReader.Create("path/to/your/document.xml", readerSettings)) { while (reader.Read()) { // Process the XML document if needed } } } catch (XmlSchemaException ex) { Console.WriteLine("Validation error: " + ex.Message); } catch (Exception ex) { Console.WriteLine("An error occurred: " + ex.Message); } } // Callback function for validation events private static void ValidationCallBack(object sender, ValidationEventArgs args) { if (args.Severity == XmlSeverityType.Warning) { Console.WriteLine("\tWarning: {0}", args.Message); } else if (args.Severity == XmlSeverityType.Error) { Console.WriteLine("\tError: {0}", args.Message); } } }

Related Topics