|
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
29.20.1. Runtime XML Schema ValidatieIndien je je eigen applicatie at runtime XML informatie wil laten valideren tegen bepaalde schemas dan is dit mogelijk.
Laat hiervoor een System.Xml.XmlReader werken volgens de settings van een System.Xml.XmlReaderSettings object. En geeft in dit object in wat de vorm van validatie is ( bijvoorbeeld System.Xml.ValidationType.Schema ), en wat het schema en eventuele targetnamespace is :
XMLSchema1.xsd : XML Schema Definition <?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="ProductSchema">
<element name="product">
<complexType>
<sequence>
<element name="name" type="string"/>
<element name="price" type="decimal"/>
</sequence>
<attribute name="id" type="int" use="required"/>
</complexType>
</element>
</schema>XMLFile1.xml : XML Instantie <?xml version="1.0" encoding="utf-8" ?>
<ps:product xmlns:ps="ProductSchema" id="101">
<name>Bear</name>
<price>2.05</price>
</ps:product> Visual Basic 2010 Broncode Class XMLSchemaRuntimeValidationExample Public Shared Sub Main() Dim settings As New System.Xml. XmlReaderSettings settings.ValidationType = Xml.ValidationType.Schema settings.Schemas.Add( "ProductSchema", "XMLSchema1.xsd") Dim reader As System.Xml. XmlReader = _ System.Xml.XmlReader.Create( "XMLFile1.xml", settings) Try Do While reader.Read : Loop Console.WriteLine( "Validated successfully.") Catch ex As System.Xml.Schema. XmlSchemaValidationException Console.WriteLine( "XmlSchemaValidationException occurred :") Console.WriteLine( ".Message : " & ex.Message) Console.WriteLine( ".Line : " & ex.LineNumber) Console.WriteLine( ".Position : " & ex.LinePosition) End Try reader.Close() Console.ReadLine() End SubEnd ClassDownload Visual Basic 2010 Broncode Download Visual C# Sourcecode
Console Application Output Validated successfully. Voeren we dit voorbeeld uit met XMLFile2.xml die het id attribuut mist : XML Instantie <?xml version="1.0" encoding="utf-8" ?>
<ps:product xmlns:ps="ProductSchema">
<name>Bear</name>
<price>2.05</price>
</ps:product> Dan krijgen we volgende output : Console Application Output XmlSchemaValidationException occurred :
.Message : The required attribute 'id' is missing.
.Line : 2
.Position : 2 Voeren we dit voorbeeld uit met XMLFile3.xml die geen <name> element bevat : XML Instantie <?xml version="1.0" encoding="utf-8" ?>
<ps:product xmlns:ps="ProductSchema" id="101">
<price>2.05</price>
</ps:product> Dan krijgen we volgende output : Console Application Output XmlSchemaValidationException occurred :
.Message : The element 'product' in namespace 'ProductSchema' has invalid child
element 'price'. List of possible elements expected: 'name'.
.Line : 3
.Position : 4 Voeren we dit voorbeeld uit met XMLFile4.xml waar <price> geen valide Decimal waarde is : XML Instantie <?xml version="1.0" encoding="utf-8" ?>
<ps:product xmlns:ps="ProductSchema" id="101">
<name>Bear</name>
<price>xyz</price>
</ps:product> Dan krijgen we volgende output : Console Application Output XmlSchemaValidationException occurred :
.Message : The 'price' element is invalid - The value 'xyz' is invalid accordin
g to its datatype 'http://www.w3.org/2001/XMLSchema:decimal' - The string 'xyz'
is not a valid Decimal value.
.Line : 4
.Position : 15
Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.
|