Edit

Share via


Extensions Class

Definition

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

C#
public static class Extensions
Inheritance
Extensions

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. 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");  

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  

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.

Methods

GetSchemaInfo(XAttribute)

Gets the post-schema-validation infoset (PSVI) of a validated attribute.

GetSchemaInfo(XElement)

Gets the post-schema-validation infoset (PSVI) of a validated element.

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).

Validate(XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)

This method validates that an XAttribute conforms to a specified XmlSchemaObject and an XmlSchemaSet.

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).

Validate(XDocument, XmlSchemaSet, ValidationEventHandler)

This method validates that an XDocument conforms to an XSD in an XmlSchemaSet.

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).

Validate(XElement, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)

This method validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet.

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1