Validates that an XElement sub-tree conforms to a specified XmlSchemaObject and an XmlSchemaSet.
Imports System
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Schema
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:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='GrandChild1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='GrandChild2' minOccurs='1' maxOccurs='2'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</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>
<Child1>
<GrandChild1>gc</GrandChild1>
<GrandChild2>gc</GrandChild2>
</Child1>
</Root>
Console.WriteLine("Validating doc1 ...")
errors = False
doc1.Validate(schemas, AddressOf XSDErrors, True)
Console.WriteLine("doc1 {0}", IIf(errors, "did not validate", "validated"))
End Sub
End Class
Related examples in the same category