Extensions.Validate validates that an XAttribute conforms to a specified XmlSchemaObject and an XmlSchemaSet.
Imports System
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.XPath
Public Class MainClass
Shared errors As Boolean = False
Private Shared Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
Console.WriteLine("{0}", e.Message)
errors = True
End Sub
Public Shared 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#'/>
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
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"))
lang.Value = "VC"
errors = False
lang.Validate(lang.GetSchemaInfo().SchemaAttribute, schemas, AddressOf XSDErrors)
Console.WriteLine("lang {0}", IIf(errors, "did not validate", "validated"))
End Sub
End Class
Related examples in the same category