Contents Overview 1 Lesson: Examining Schemas 2 Lesson: Validating XML While Parsing 7 Lesson: Advanced Validation 23 Review 32 Lab 3.1: Validating XML 34 Module 3: Validating XML Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. ©2002 Microsoft Corporation. All rights reserved. Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDN, PowerPoint, SQL Server, Visual Basic, Visual C#, and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module 3: Validating XML iii Instructor Notes After completing this module, students will be able to: Instantiate an XML validating reader. Associate one or more XML schemas with an XML validating reader. Handle errors that result when a validating reader processes invalid XML. Create an application that uses the Microsoft ® .NET Framework XML classes and XML schemas to validate an XML fragment. Resolve a Uniform Resource Identifier (URI) reference to a schema or Document Type Definition (DTD). Create an application that uses the .NET Framework XML classes and an XML Schema Definition (XSD) schema or a DTD to validate well-formed XML. To teach this module, you need the following materials: Microsoft PowerPoint ® file 2663A_03.ppt Code samples file 2663A_03_Code.htm To prepare to effectively teach this module: Read the following Microsoft Visual Studio ® Help topics: • Walkthrough: Creating an XML File with an Associated XML Schema • XML Schemas and Data • XML Data Types Reference • Authoring and Using Custom Schemas Backgrounder • Validation of XML with Schemas Read the entire module. Complete the practices and the lab. Practice delivering the demonstrations. Set Visual Studio .NET display options so that XML and HTML will display for optimum visibility in the classroom. Change the display options 1. On the Tools menu, click Options. 2. Click the Text Editor folder, and then click the HTML/XML folder. 3. Select the Word wrap and Line numbers options. 4. Click the Environment folder, and then click the Fonts and Colors folder. 5. Change the font used for the Text Editor and Text Output Tool windows to Lucida Console typeface 14 point. 6. Click OK. 7. Close and restart Visual Studio .NET. Presentation: 45 minutes Lab: 45 minutes Required materials Preparation tasks iv Module 3: Validating XML In this module, some of the Microsoft PowerPoint ® slides provide hyperlinks that open a code samples page in the Web browser. The code samples page provides a way to show and discuss code samples when there is not enough space for the code on the PowerPoint slide. It also allows students to copy code samples directly from the browser window and paste them into a development environment. All of the linked code samples for this module are in a single .htm file. To open a code sample, click the appropriate hyperlink on the slide. To navigate between code samples in a particular language, use the table of contents provided at the top of the code page. Each hyperlink opens a separate instance the Web browser, so it is a good practice to click Back in Microsoft Internet Explorer after viewing a code sample. This will close the browser window and return you to the PowerPoint presentation. Hyperlinked Code Examples Module 3: Validating XML v How to Teach This Module This section contains information that will help you to teach this module. Lesson: Examining Schemas This section describes the instructional methods for teaching each topic in this lesson. The goal of this topic is to reacquaint learners with the basics of validation. Remind learners that the .NET Framework provides classes to validate XML by using DTDs or XML schemas. Quickly provide a definition of the XSD schema. Lesson: Validating XML While Parsing This section describes the instructional methods for teaching each topic in this lesson. This demonstration is based on a custom add-in. Demonstrate how to use this add-in to validate XML by using an XSD schema, and then do the same by using a DTD. Point out how both operations use the XmlValidatingReader class. In the code examinations that follow, you will have the opportunity to explain in greater detail how the underlying code works. Introduce the XmlValidatingReader class by pointing out that it is derived from the XmlReader class. Next, outline the four major programming tasks that are required to use an XmlValidatingReader. Use the programming tasks, listed in the slide and below, to provide learners with an overview of the coding required to create a simple validating reader. The following tasks are covered in the next four topics: 1. Instantiate a validating reader. 2. Associate one or more schemas with the validating reader. 3. Create a validation event handler. 4. Process nodes by calling one of the Read() methods. Show the code that instantiates a validating reader. Show the code that allows you to associate one or more schema with a validating reader. Show the code that implements an error-handling routine. Show the code that allows you to poll data from validation errors. Overview of Validation What Is an XML Schema? Demonstration: Validatin g XML Introduction to the XmlValidatingReader Class How to Instantiate a Validatin g Reader How to Associate Schemas with a Validating Reade r How to Create a Validation Event Handler How to Get Details About Validation Errors vi Module 3: Validating XML This code examination shows learners how an add-in works. More importantly, it shows the inner workings of a simple validation application. In the code examination that follows this one, you will explain code that uses a validating reader and a DTD. This practice covers the four previously described coding tasks. Because these tasks are central to creating a generic validating reader, the practice is comprehensive in its scope and borders on the scale of a mini-lab. You should therefore allow learners plenty of time and offer assistance to complete this practice. Lesson: Advanced Validation This section describes the instructional methods for teaching each topic in this lesson. Show code that resolves a DTD resource used by XmlValidatingReader. Show code that handles cases where only a fragment of XML must be validated, instead of an entire XML document. Show the code that validates XML by using a DTD instead of an XSD schema. Explain the code that performs DTD validation on an XML document not conforming to the DTD named by the validating reader. Review Several multiple-choice review questions appear at the end of the module. Review these questions with your students before proceeding to the lab. Customization Information This section identifies the lab setup requirements for a module and the configuration changes that occur on student computers during the labs. This information is provided to assist you in replicating or customizing Microsoft Official Curriculum (MOC) courseware. Lab Setup There are no lab setup requirements that affect replication or customization. Lab Results There are no configuration changes on student computers that affect replication or customization. Code Examination: Validating XML Using an XSD Schema Practice: Validating XML How to Resolve External Resources How to Validate XML Fra gments How to Validate XML Using a DTD Code Examination: Validating XML Using a DTD Module 3: Validating XML 1 Overview Examining Schemas Validating XML While Parsing Advanced Validation ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** This module gives you an overview of XML Schema Definition (XSD) data types and then covers how to use the Microsoft ® .NET Framework Extensible Markup Language (XML) classes to perform XML schema validation. The lesson Advanced Validation teaches you how to resolve external entities, how to validate XML fragments, and how to validate XML by using a Document Type Definition (DTD). After completing this module, you will be able to: Instantiate an XML validating reader. Associate one or more XML schemas with an XML validating reader. Handle errors that result when a validating reader processes invalid XML. Create an application that uses the .NET Framework XML classes and XML schemas to validate an XML fragment. Resolve a Uniform Resource Identifier (URI) reference to a schema or DTD. Create an application that uses the .NET Framework XML classes and an XSD schema or a DTD to validate well-formed XML. Introduction Objectives 2 Module 3: Validating XML Lesson: Examining Schemas Overview of Validation What Is an XML Schema? ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The lesson provides an overview of validation and XML schema data types. After completing this lesson, you will be able to: Describe the relationship between valid XML and well-formed XML. Describe why XML validation is important and when it should be used. Explain the differences between the three categories of schema data types. Describe how XML schemas validate instance documents. Introduction Lesson ob jectives Module 3: Validating XML 3 Overview of Validation What is XML validation? The process of comparing XML structure, names, and data types to a set of requirements A schema specifies these requirements Well-formed XML is a validation prerequisite Why validate? To filter data shared between organizations and applications To protect databases against invalid data types To standardize data structures The .NET Framework supports these validation technologies: XSD, XDR, and DTD ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Validating XML is the process of comparing an XML source to a predefined set of requirements. A prerequisite for validation is well-formedness. Validation requirements are defined in terms of names of elements and attributes, the nesting structure of elements, and the type of data that can occur in attributes and elements. An XML schema is a well-formed XML document that describes the XML structure and data types of another XML document. An XML document that conforms to the description of XML contained in a schema is said to be a valid instance of that schema. You use a schema to ensure that XML adheres to a specified structure and data type. There are three major reasons to validate XML: To filter data shared between organizations and applications To protect databases against invalid data types To standardize data structures What is XML validation? Why validate? 4 Module 3: Validating XML The .NET Framework supports XSD schemas and, for backward compatibility, DTD and XML-Data Reduced (XDR) schemas. These validation technologies are described in the following table. Validation technology Description Document Type Definition (DTD) DTD is the first proposed World Wide Web Consortium (W3C) schema technology. Although you might still find DTDs in use, they are considered an obsolete technology because they are not expressed in XML and because they provide limited support for specifying data types. XML-Data Reduced (XDR) An interim Microsoft schema technology based on XML. XML Schema Definition (XSD) The current W3C standard, based on XML. .NET Framework support for validation technolo gies [...]... Reference Module 3: Validating XML 7 Lesson: Validating XML While Parsing Demonstration: Validating XML Introduction to the XmlValidatingReader Class How to Instantiate a Validating Reader How to Associate Schemas with a Validating Reader How to Create a Validation Event Handler How to Get Details About Validation Errors Code Examination: Validating XML Using an XSD Schema Practice: Validating XML While... ValidationType.Schema // C# XmlTextReader BooksReader = new XmlTextReader(@"c:\books .xml" ); XmlValidatingReader BooksValidater = new XmlValidatingReader(BooksReader); BooksValidater.ValidationType = ValidationType.Schema; 12 Module 3: Validating XML How to Associate Schemas with a Validating Reader Associate schemas statically Set the schemaLocation in the XML document . complex t ype Module 3: Validating XML 7 Lesson: Validating XML While Parsing Demonstration: Validating XML Introduction to the XmlValidatingReader. validates XML against multiple schemas while it parses. Introduction Lesson ob jectives 8 Module 3: Validating XML Demonstration: Validating XML Use an XmlValidatingReader