30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 18 Extensible Markup Language (XML) 867<br />

contain child elements. Setting the model attribute <strong>to</strong> "closed" specifies that a conforming<br />

XML document can contain only elements defined in this Schema. Line 10 defines<br />

element book; this element’s content is “elements only” (i.e., eltOnly). This means<br />

that the element cannot contain mixed content (i.e., text and other elements). Within the ElementType<br />

element named book, the element element indicates that title is a child<br />

element of book. Attributes minOccurs and maxOccurs are set <strong>to</strong> "1", indicating that<br />

a book element must contain exactly one title element. The asterisk (*) in line 15 indicates<br />

that the Schema permits any number of book elements in element books.<br />

Class XmlValidatingReader validates an XML document against a Schema. The<br />

program in Fig. 18.18 validates an XML document that the user provides (such as,<br />

Fig. 18.16 or Fig. 18.19) against a Microsoft Schema document (Fig. 18.17).<br />

Line 17 creates an XmlSchemaCollection reference named schemas. Line 33<br />

calls its Add method <strong>to</strong> add an XmlSchema object <strong>to</strong> the Schema collection. Method Add<br />

receives as arguments a name that identifies the Schema (e.g., "book") and the name of<br />

the Schema file (e.g., "book.xdr").<br />

The XML document <strong>to</strong> be validated against the Schema(s) contained in the<br />

XmlSchemaCollection must be passed <strong>to</strong> the XmlValidatingReader construc<strong>to</strong>r<br />

(line 48–49). Lines 44–45 create an XmlReader for the file that the user selected<br />

from filesComboBox. The XmlReader passed <strong>to</strong> this construc<strong>to</strong>r is created using the<br />

file name selected from cboFiles (lines 44–45).<br />

Line 52 Adds the Schema collection referenced by Schemas <strong>to</strong> the Schemas property.<br />

This property sets the Schema(s) used <strong>to</strong> validate the document. The Validation-<br />

Type property (line 55) is set <strong>to</strong> the ValidationType enumeration constant for<br />

Microsoft Schema. Lines 58–59 register method ValidationError with ValidationEventHandler.<br />

Method ValidationError (lines 79–84) is called if the document<br />

is invalid or an error occurs, such as if the document cannot be found. Failure <strong>to</strong><br />

register a method with ValidationEventHandler causes an exception <strong>to</strong> be thrown<br />

when the document is missing or invalid.<br />

1 ' Fig. 18:18: ValidationTest.vb<br />

2 ' Validating XML documents against Schemas.<br />

3<br />

4 Imports System.Windows.Forms<br />

5 Imports System.Xml<br />

6 Imports System.Xml.Schema ' contains Schema classes<br />

7<br />

8 ' determines XML document Schema validity<br />

9 Public Class FrmValidationTest<br />

10 Inherits Form<br />

11<br />

12 ' Controls for validating XML document<br />

13 Friend WithEvents cboFiles As ComboBox<br />

14 Friend WithEvents cmdValidate As But<strong>to</strong>n<br />

15 Friend WithEvents lblConsole As Label<br />

16<br />

17 Private schemas As XmlSchemaCollection ' Schemas<br />

18 Private valid As Boolean ' validation result<br />

Fig. 18.18 Schema-validation example (part 1 of 3).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!