Extensions::Validate Method (XAttribute^, XmlSchemaObject^, XmlSchemaSet^, ValidationEventHandler^, Boolean)
Assembly: System.Xml.Linq (in System.Xml.Linq.dll)
public: [ExtensionAttribute] static void Validate( XAttribute^ source, XmlSchemaObject^ partialValidationType, XmlSchemaSet^ schemas, ValidationEventHandler^ validationEventHandler, bool addSchemaInfo )
An XmlSchemaObject that specifies the sub-tree to validate.
A ValidationEventHandler for an event that occurs when the reader encounters validation errors. If null, throws an exception upon validation errors.
Thrown for XML Schema Definition Language (XSD) validation errors.
You can use this method to validate that an XAttribute conforms to a schema. You typically use this method when you have modified an attribute, and you want to make sure that it still conforms to its schema. You could validate the entire document, but it takes less processing time to validate just the attribute.
If addSchemaInfo is true, this method populates the attribute with the post-schema-validation infoset (PSVI). After you have populated the XML tree with the PSVI, you can call Extensions::GetSchemaInfo on the validated attribute. This is useful if you are writing code that relies on data returned by GetSchemaInfo.
If you pass null for validationEventHandler, then this method raises an exception upon validation errors. Validation warnings will not raise an exception.
To validate an attribute, you use an instance of XmlSchemaObject. You can obtain this instance in various ways. An easy way is as follows:
Validate that a document conforms to a schema.
Add the post-schema-validation infoset (PSVI) by calling the Validate extension method.
After you have an instance of an XmlSchemaObject, you can use this method to validate an attribute.
This example produces the following output:
Validating doc1 ... doc1 validated Validating Lang attribute ... lang validated Validating Lang attribute ... The 'Lang' attribute is invalid - The value 'VC' is invalid according to its datatype 'Token' - The Enumeration constraint failed. lang did not validate Invalid Attribute /Root/@Lang
Available since 3.5