Extensions Class (System.Xml.Schema)

Switch View :
ScriptFree
.NET Framework Class Library
Extensions Class

This class contains the LINQ to XML extension methods for XSD validation.

Inheritance Hierarchy

System.Object
  System.Xml.Schema.Extensions

Namespace:  System.Xml.Schema
Assembly:  System.Xml.Linq (in System.Xml.Linq.dll)
Syntax

Visual Basic
<ExtensionAttribute> _
Public NotInheritable Class Extensions
C#
public static class Extensions
Visual C++
[ExtensionAttribute]
public ref class Extensions abstract sealed
F#
[<AbstractClass>]
[<Sealed>]
type Extensions =  class end
Methods

  Name Description
Public method Static member GetSchemaInfo(XAttribute) Gets the post-schema-validation infoset (PSVI) of a validated attribute.
Public method Static member GetSchemaInfo(XElement) Gets the post-schema-validation infoset (PSVI) of a validated element.
Public method Static member Validate(XDocument, XmlSchemaSet, ValidationEventHandler) This method validates that an XDocument conforms to an XSD in an XmlSchemaSet.
Public method Static member Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler) This method validates that an XAttribute conforms to a specified XmlSchemaObject and an XmlSchemaSet.
Public method Static member Validate(XDocument, XmlSchemaSet, ValidationEventHandler, Boolean) Validates that an XDocument conforms to an XSD in an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).
Public method Static member Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler) This method validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet.
Public method Static member Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean) Validates that an XAttribute conforms to a specified XmlSchemaObject and an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).
Public method Static member Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler, Boolean) Validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet, optionally populating the XML tree with the post-schema-validation infoset (PSVI).
Top
Remarks

This class also contains methods to get the post-schema-validation infoset (PSVI) of a validated XML node.

When you validate an XDocument, XElement, or XAttribute, you can also optionally populate the XML tree with the post-schema-validation infoset. PSVI information is added as an annotation of type System.Xml.Schema.XmlSchemaInfo.

Examples

The XSD in Sample XSD File: Customers and Orders contains a schema that can be used to validate the XML document in Sample XML File: Customers and Orders (LINQ to XML). The following example loads the schema and the document, validates the document, changes the document so that the xs:key and xs:keyref relationship is not valid, and then attempts to validate again.

C#
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", "CustomersOrders.xsd");

XDocument custOrd = XDocument.Load("CustomersOrders.xml");

Console.WriteLine("Validating custOrd");
bool errors = false;
custOrd.Validate(schemas, (o, e) =>
                     {
                         Console.WriteLine("{0}", e.Message);
                         errors = true;
                     });
Console.WriteLine("custOrd {0}", errors ? "did not validate" : "validated");

// Modify the custOrd tree so that it is no longer valid.
custOrd.Root.Element("Orders").Element("Order").Element("CustomerID").Value = "AAAAA";

Console.WriteLine();
Console.WriteLine("Validating custOrd");
errors = false;
custOrd.Validate(schemas, (o, e) =>
                     {
                         Console.WriteLine("{0}", e.Message);
                         errors = true;
                     });
Console.WriteLine("custOrd {0}", errors ? "did not validate" : "validated");
Visual Basic
Dim errors As Boolean = False

Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
    Console.WriteLine("{0}", e.Message)
    errors = True
End Sub

Sub Main()
    Dim schemas As XmlSchemaSet = New XmlSchemaSet()
    schemas.Add("", "CustomersOrders.xsd")

    Console.WriteLine("Validating custOrd")
    Dim custOrd As XDocument = XDocument.Load("CustomersOrders.xml")
    errors = False
    custOrd.Validate(schemas, AddressOf XSDErrors)
    Console.WriteLine("custOrd {0}", IIf(errors, "did not validate", "validated"))

    Console.WriteLine()
    Console.WriteLine("Validating custOrd")
    ' Modify the source document so that it will not validate.
    custOrd.Root.Element("Orders").Element("Order").Element("CustomerID").Value = "AAAAA"
    errors = False
    custOrd.Validate(schemas, AddressOf XSDErrors)
    Console.WriteLine("custOrd {0}", IIf(errors, "did not validate", "validated"))
End Sub

This example produces the following output:

Validating custOrd
custOrd validated

Validating custOrd
The key sequence 'AAAAA' in Keyref fails to refer to some key.
custOrd did not validate
Version Information

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
See Also

Reference

Community Content

Dan Oren
WP7 Support
How can I use the schema validation in Windows Phone 7 ? $0You can't add a reference to this dll in WP7 project so the Validation method is missing from the XDocument object.$0