Export (0) Print
Expand All

Extensions.Validate Method (XAttribute, XmlSchemaObject, XmlSchemaSet, ValidationEventHandler)

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

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

'Declaration
<ExtensionAttribute> _
Public Shared Sub Validate ( _
	source As XAttribute, _
	partialValidationType As XmlSchemaObject, _
	schemas As XmlSchemaSet, _
	validationEventHandler As ValidationEventHandler _
)

Parameters

source
Type: System.Xml.Linq.XAttribute

The XAttribute to validate.

partialValidationType
Type: System.Xml.Schema.XmlSchemaObject

An XmlSchemaObject that specifies the sub-tree to validate.

schemas
Type: System.Xml.Schema.XmlSchemaSet

An XmlSchemaSet to validate against.

validationEventHandler
Type: System.Xml.Schema.ValidationEventHandler

A ValidationEventHandler for an event that occurs when the reader encounters validation errors. If Nothing, throws an exception upon validation errors.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type XAttribute. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

ExceptionCondition
XmlSchemaValidationException

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 you pass Nothing for validationEventHandler, 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:

  1. Validate that a document conforms to a schema.

  2. Add the post-schema-validation infoset (PSVI) by calling the Validate extension method.

  3. Call the GetSchemaInfo extension method to retrieve an object that implements IXmlSchemaInfo. From the retrieved object, you can get an XmlSchemaObject.

After you have an instance of an XmlSchemaObject, you can use this method to validate an attribute.

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 xsdMarkup As XDocument = _
      <?xml version='1.0'?>
      <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
          <xsd:element name='Root'>
              <xsd:complexType>
                  <xsd:simpleContent>
                      <xsd:extension base='xsd:string'>
                          <xsd:attribute name='Lang' use='required'>
                              <xsd:simpleType>
                                  <xsd:restriction base='xsd:token'>
                                      <xsd:enumeration value='C#'/>
                                      <xsd:enumeration value='VB'/>
                                  </xsd:restriction>
                              </xsd:simpleType>
                          </xsd:attribute>
                      </xsd:extension>
                  </xsd:simpleContent>
              </xsd:complexType>
          </xsd:element>
      </xsd:schema>

    Dim schemas As XmlSchemaSet = New XmlSchemaSet()
    schemas.Add("", xsdMarkup.CreateReader)

    Dim doc1 As XDocument = <?xml version='1.0'?>
                            <Root Lang='C#'/>

    Console.WriteLine("Validating doc1 ...")
    errors = False
    doc1.Validate(schemas, AddressOf XSDErrors, True)
    Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))

    Console.WriteLine()
    Console.WriteLine("Validating Lang attribute ...")
    Dim lang As XAttribute = doc1.Root.Attribute("Lang")

    errors = False
    lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors)
    Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))

    ' the following makes the Lang attribute invalid according to the schema
    lang.Value = "VC"

    Console.WriteLine()
    Console.WriteLine("Validating Lang attribute ...")

    errors = False
    lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors)
    Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
End Sub

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

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft