In the code example that follows, a schema-validating Xml Reader validates the XML data loaded into the DOM. Schema Class Xml Document Validation Example Shared Sub Main() Try ' Create a schema validating Xml Reader. Validation Event Handler, New Validation Event Handler(Address Of Validation Event Handler) settings. Select Single Node("/bk:bookstore/bk:book/bk:price", manager) Console. Invalid modifications are made to the XML document and the document is then revalidated, causing schema validation errors. Dim settings As Xml Reader Settings = New Xml Reader Settings() settings.
If a Validation Event Handler was assigned to the Xml Reader Settings instance, or passed to the Validate method of the Xml Document class, the Validation Event Handler will handle schema validation errors; otherwise an Xml Schema Validation Exception is raised when a schema validation error is encountered. In this example, the XML document is not validated as it is loaded into the DOM, but rather is validated after it has been loaded into the DOM using the Validate method of the Xml Document class. The example below is similar to the example in "Validating an XML Document As It Is Loaded into the DOM" above. The Xml Document class validates the XML data as it is loaded into the DOM when a validating Xml Reader is passed to the Load method of the Xml Document class. Create("contoso Books.xml", settings) ' The Xml Document validates the XML document contained ' in the Xml Reader as it is loaded into the DOM. After successful validation, schema defaults are applied, text values are converted to atomic values as necessary, and type information is associated with validated information items. Dim document As Xml Document = New Xml Document() document. Inner Xml = "A" Dim price Nodes As Xml Node List = document. Select Single Node("/bk:bookstore/bk:book/bk:price", manager) Console.
Finally, one of the errors is corrected, and then part of the XML document is partially validated. Load("contoso Books.xml") ' Add the XML schema for the XML document to the ' Schemas property of the Xml Document. Dim manager As Xml Namespace Manager = New Xml Namespace Manager(document. Add Namespace("bk", " Dim price Node As Xml Node = document.
For examples illustrating the Validation Event Handler, see "Validating an XML Document As It Is Loaded into the DOM" and "Validating an XML Document in the DOM" above.
Message) End Try End Sub Shared Sub Validation Event Handler(By Val sender As Object, By Val args As Validation Event Args) If args. Schema validation warnings are not reported unless the Report Validation Warnings flag is specified to the Xml Reader Settings object.
Finally, one of the errors is corrected, and then part of the XML document is partially validated.
The last book ' element is invalid, but not included in validation. Select Single Node("/bk:bookstore/bk:book", manager) document. Validate(Address Of Validation Event Handler, book Node) Catch ex As Xml Exception Console.
Validation errors are handled by the Validation Event Handler.