1. Trang chủ
  2. » Công Nghệ Thông Tin

Module 5: Reading and Writing XML with ADO.NET

54 1,4K 8
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 54
Dung lượng 0,97 MB

Nội dung

Module 5: Reading and Writing XML with ADO.NET Contents Overview 1 Lesson: Creating XSD Schemas 2 Lesson: Loading Schemas and Data into DataSets 14 Lesson: Writing XML from a DataSet 25 Review 33 Lab 5.1: Working with XML Data in ADO.NET 35 Course Evaluation 43 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, places or events 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.  2001-2002 Microsoft Corporation. All rights reserved. Microsoft, MS-DOS, Windows, Windows NT, Win32, Active Directory, ActiveX, BizTalk, IntelliSense, JScript, MSDN, SQL Server, Visual Basic, Visual C#, Visual C++, Visual J#, Visual Studio, and Windows Media are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners. Module 5: Reading and Writing XML with ADO.NET iii Instructor Notes Microsoft ® ADO.NET uses XML as the format for managing and moving data from a data source to a DataSet object and back. This module explains how to use the XML Schema Definition Language (XSD) to create files that provide structure for working with data in XML documents and DataSets. After completing this module, students will be able to: ! Generate an XSD schema from a DataSet by using graphical tools. ! Save a DataSet structure to an XSD schema file. ! Create and populate a DataSet from an XSD schema and XML data. ! Save DataSet data as XML. ! Write and load changes by using a DiffGram. To teach this module, you need the following materials: ! Microsoft PowerPoint ® file 2389B_05.ppt ! Module 5, “Reading and Writing XML with ADO.NET” ! Lab 5.1, Working with XML Data in ADO.NET To prepare for this module: ! Read all of the materials for this module. ! Complete the practices and labs. ! Read the latest .NET Development news at http://msdn.microsoft.com/library/default.asp?url=/nhp/ Default.asp?contentid=28000519 This module contains code examples that are linked to text hyperlinks at the bottom of PowerPoint slides. These examples enable you to teach from code examples that are too long to display on a slide. All of the code examples for this module are contained in one .htm file. Students can copy the code directly from the browser or from the source, and paste it into a development environment. To display a code sample, click a text hyperlink on a slide. Each link opens an instance of Microsoft Internet Explorer and displays the code associated with the link. At the end of each example is a link that displays a table of contents of all examples in this module. After you have finished teaching the code for an example, close the instance of the browser to conserve resources. If time constraints occur, one or more practices in this module can be presented as instructor-led demonstrations. Required materials Preparation tasks Hyperlinked code examples Practices iv Module 5: Reading and Writing XML with ADO.NET How to Teach This Module This section contains information that will help you to teach this module. Lesson: Creating XSD Schemas This section describes the instructional methods for teaching each topic in this lesson. Technical Notes: The example of PersonPet.xsd was chosen because the structure closely resembles two tables in a relational database and can be easily understood by students who have experience with relational databases. A more typical representation of this data would contain child information for pets as a nested complex type inside the Person elements. Spend only a few minutes explaining the code sample. A more typical example is given later in the module. Transition to Practice Exercise: In this practice, you will create an XML file based on a schema by using the Microsoft Visual Studio ® XML Editor. Instruct students to turn to the practice exercise at the end of this topic in the student workbook. Guide the students through the practice. Practice Solution: There is no solution provided. The purpose of the exercise is to give students experience in using the XML Designer to create an XML file. After the Practice: • When would you use an XSD schema, and when would you create the DataSet structure programmatically? Technical Notes: This section defines a typed DataSet as one that inherits tables and relationships from another DataSet class. Be prepared to discuss the uses for an abstract base class if students are not familiar with object-oriented programming techniques. Discussion Questions: Personalize the following question to the background of the students in your class. • What are the advantages of using typed DataSets? Technical Notes: The code sample in this section represents a typical hierarchical document. The purpose is to identify structures that will translate to relational tables and objects. Walk through this example to help students understand what happens when the ReadXML method is used to infer a schema. What Is an XSD Schema? What Are Typed DataSets? How XSD Schema Information Maps to Relational Structure Module 5: Reading and Writing XML with ADO.NET v Transition to Practice Exercise: Instruct students to turn to the practice exercise at the end of this topic in the student workbook. The purpose of this practice is to give students experience in using the Visual Studio .NET Schema Editor to create and edit XSD schema documents. Practice Solution: There is no solution for this practice. The purpose of this practice is to show how simple it is to create a valid XSD schema when using Visual Studio designers. After the Practice: • What are the advantages of using the Visual Studio .NET graphical tools when creating XSD schema documents? Generating an XSD Schema with Visual Studio vi Module 5: Reading and Writing XML with ADO.NET Lesson: Loading Schemas and Data into DataSets This section describes the instructional methods for teaching each topic in this lesson. Discussion Questions: Personalize the following question to the background of the students in your class. • What are the advantages and disadvantages of the three different ways to load a schema into a DataSet? Discussion Questions: Personalize the following questions to the background of the students in your class. ! In the example for loading an XSD schema by using a Stream object, what is the name of the schema file? ! What exceptions are handled in this example? Technical Notes: Teach this topic quickly and do not review the code sample. Most of the significant points are covered in the following demonstration. Discussion Questions: Personalize the following questions to the background of the students in your class. ! How is the ParseSchema procedure programmed to load the DataSet with schema information from PersonPet.xsd? ! In this demonstration, what DataSet properties and methods are used to retrieve information about tables in the DataSet? Technical Notes: Discuss each of the XMLReadMode options. Point out that although the auto option is listed, it is poor programming practice to use this rather than specifying a read mode. Also point out that attempts to read data that do not match DataSet schemas will not work. The data will simply not be loaded into the DataSet. No error is generated. Discussion Questions: Personalize the following question to the background of the students in your class. • In the example of loading a schema and data into a DataSet, what method is used? What option of the XmlReadMode parameter is used and what does that option do? Loading a Schema into a DataSet Loading XSD Information from A File Examinin g Metadata Demonstration: Examining DataSet Structure Loading XML Data into a DataSet Module 5: Reading and Writing XML with ADO.NET vii Lesson: Writing XML from a DataSet This section describes the instructional methods for teaching each topic in this lesson. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are the differences between using WriteXmlSchema and GetXmlSchema? ! When would you use WriteXmlSchema and when would you use GetXmlSchema? Discussion Questions: Personalize the following question to the background of the students in your class. • In the example of writing XML data to a file, what would you need to add for this example to also write schema information? The purpose of this demonstration is to show how to save schema and data information from a DataSet. This example loads data into tables and generates an XML data file. Technical Notes: Be prepared to discuss scenarios where DiffGrams are useful. Point out that ADO DiffGrams contain original data in addition to changes. Writing a Schema to a File, Reader, or Stream Writing DataSet Information to a File or Stream Demonstration: Writing an Inline Schema and Data to a File Writing DataSet Changes viii Module 5: Reading and Writing XML with ADO.NET Appendix G: Using the XmlDataDocument Object This optional, teachable appendix introduces the XMLDataDocument object. In general, this object is useful for treating relational data as if it were XML data. You typically use the XMLDataDocument object to apply XML techniques such as filtering with a style sheet or an XML Path Language (XPath) query. After completing this lesson, students will be able to: ! Identify the purpose and uses of the XmlDataDocument object. ! Manipulate data in an XmlDataDocument object. This lesson is optional because students might be unfamiliar with XSD style sheets, XPath queries, and Document Object Model (DOM) node manipulation. If the students have the appropriate XML background, you can choose to present Appendix G at the end of Module 5, “Reading and Writing XML with ADO.NET” in Course 2389B, Programming with Microsoft ADO.NET. Technical Notes: Emphasize that the XMLDataDocument object allows you to manipulate the same data by using relational techniques or XML techniques. Technical Notes: Point out that you must synchronize a DataSet with an XMLDataDocument object when you create the XMLDataDocument. You cannot set the DataSet property of the XMLDataDocument after creation. This property is read-only. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What does providing a hierarchical view of existing relational data allow you to do in your applications? ! What are some examples of XML-related operations that you might want to perform on the data? Discussion Questions: Personalize the following question to the background of the students in your class. • What are some examples of relational operations that you can perform after you provide a relational view of XML data? When would you want to do this? What Is an XmlDataDocument Object? Synchronizing an XmlDataDocument with a DataSet Providing a Hierarchical View of Existing Relational Data How to Provide a Relational View of XML Data Module 5: Reading and Writing XML with ADO.NET ix Technical Notes: ! This topic is complex. Be prepared to discuss the XSLTransform object and the XMLTextWriter object. Stress that the process and code for applying a style sheet will be almost identical for most cases. It is important for students to recognize the importance of being able to apply a style sheet to relational data. Do not spend more than three minutes explaining the code sample. ! An alternate way to accomplish this task is to save the XML data to a file and generate a schema to describe the data. It is a simple process to apply a style sheet to the XML file, rather than loading it into an XMLDataDocument object. Technical Notes: This is a complicated way of querying a DataSet. It is probably better to use the find methods of the dataset or some other query mechanism. This technique is included for completeness. Do not spend more than three minutes explaining the code sample. Discussion Questions: Personalize the following questions to the background of the students in your class. ! What are some advantages of using XPath queries versus using SQL statements on a DataSet? ! Are there any situations where you might prefer to use a SQL statement for a query? Applying an XSLT Style Sheet to a DataSet Performing an XPath Quer y on a DataSet x Module 5: Reading and Writing XML with ADO.NET Review: Reading and Writing XML with ADO.NET This section provides the answers to review questions at the end of this module. 1. You want to create an XSD schema from data in an XML document. The document does not contain an inline schema, and no external XSD document exists. What are the ways that you can generate a schema by using Visual Studio .NET? You can create a schema by using the XML Designer. First add the XML file to the project, and then infer a schema by using visual tools. 2. An XPath query returns results from a Microsoft SQL Server ™ database. How can you load the results into a DataSet? SQL Server generates document fragments rather than well-formed XML documents as the result of sql-xml queries. Use the ReadXml method of the DataSet object with the XmlReadMode parameter set to fragment. 3. Should you create XML files with an inline schema, or generate a separate XSD file? It depends on the situation. If you are shipping data to a client who does not know the structure of the data, it is a good idea to use an inline schema. The inline schema is easy to read or ignore. It is somewhat faster to load data and schemas from a single file rather than from separate files. If you have a choice whether to use separate files or an inline schema, use an inline schema. You might want to use separate files when you have multiple sets of information that conform to the same schema; for example, a standard automobile parts form that always looks the same. 4. You have data stored in a relational database. You want to retrieve this data and filter it by using an Extensible Stylesheet Language for Transformations (XSLT) style sheet before sending it to a client. How can you do this? First, load the data from the database into a DataSet. Next, create an XmlDataDocument object and synchronize it with the DataSet. Use the XmlDataDocument as the information source for an XSLT transformation that uses a style sheet to transform the data. If the data is stored in an XML data file to begin with, you can apply the style sheet in the browser. Because the data is stored relationally in a DataSet, you must first generate a hierarchical representation by loading the information into an XmlDataDocument object before you can transform it. [...]... following page 5 6 Module 5: Reading and Writing XML with ADO.NET 4 3 Tickles spider 5 1 Tweetie canary Module 5: Reading and Writing XML with ADO.NET Practice 7 Create an XML file based on a schema by using the Visual Studio XML Editor ! Create an XML file based.. .Module 5: Reading and Writing XML with ADO.NET 1 Overview ! Creating XSD Schemas ! Loading Schemas and Data into DataSets ! Writing XML from a DataSet *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction Microsoft® ADO.NET uses XML as the format for managing and moving data from a data source to a DataSet object and back In addition,... parameter to specify what the XML file contains and what information should be loaded from the file This parameter is optional If no XmlReadMode value is supplied, the default value Auto is used 22 Module 5: Reading and Writing XML with ADO.NET XmlReadMode parameter values The following table shows the values for the XmlReadMode parameter of the ReadXml method of the DataSet object XmlReadMode value Description... the XML file and chooses the most appropriate option 1 If the DataSet contains a schema or the XML contains an inline schema, ReadSchema is used 2 If the DataSet does not contain a schema and the XML does not contain an inline schema, InferSchema is used For best performance, specify an XmlReadMode rather than using Auto Module 5: Reading and Writing XML with ADO.NET Example of loading a schema and. .. schema in addition to XML data Private Sub ReadXmlDataAndSchema() Try myDS = New DataSet() myDS.ReadXml("C:\sampledata\PurchaseOrder .xml" , _ XmlReadMode.ReadSchema) Catch e as Exception Console.WriteLine("Exception: " & e.ToString()) End Try End Sub 23 24 Module 5: Reading and Writing XML with ADO.NET Example of inferring a schema The following example shows how to infer a schema from XML data In this example,... table 7 Run the application and observe the results 8 Quit Visual Studio NET Module 5: Reading and Writing XML with ADO.NET 21 Loading XML Data into a DataSet ! Use ReadXML to load data from a file or stream ! Simplified syntax Dataset.ReadXML(Stream | FileName | TextReader | XMLReader, { ByVal mode as XMLReadMode }) ! XMLReadMode specifies what to load " ReadSchema " IgnoreSchema " InferSchema "... not contain the same level of detail as a complete XSD schema 4 Module 5: Reading and Writing XML with ADO.NET Example of an XSD schema The following example shows a two-level XSD schema: < ?xml version="1.0" standalone="yes"?> ... information for this customer 8 Save the changes 9 Switch the XML Editor to Data view and type the name of another customer 10 Switch the XML Editor to XML view Notice that the tags and data are entered for you 8 Module 5: Reading and Writing XML with ADO.NET What Are Typed DataSets? ! Why build a typed DataSet? " " Validate data in a document " ! Save data and its definition for others to consume Text format,... the XML data ! Manually creating the structure of the DataSet by using code to build tables and create relationships 16 Module 5: Reading and Writing XML with ADO.NET Loading XSD Information from a File ! Use ReadXMLSchema to load an existing XSD schema into a DataSet ! Syntax of ReadXMLSchema DataSet.ReadXMLSchema (ByVal filename as string | stream as stream | reader as textreader | reader as xmlreader)... PurchaseOrder .xml contains XML data but no inline schema When the data is read into a DataSet, the DataSet infers a schema from the structure of the XML data Private Sub ReadXmlDataInferSchema() Try myDS = NEW DataSet() myDS.ReadXml("C:\sampledata\PurchaseOrder .xml" , _ XmlReadMode.InferSchema) Catch e as Exception Console.WriteLine("Exception: " & e.ToString()) End Try End Sub Module 5: Reading and Writing XML with . an XPath Quer y on a DataSet x Module 5: Reading and Writing XML with ADO. NET Review: Reading and Writing XML with ADO. NET This section provides the answers. Demonstration: Writing an Inline Schema and Data to a File Writing DataSet Changes viii Module 5: Reading and Writing XML with ADO. NET Appendix G: Using the XmlDataDocument

Ngày đăng: 26/10/2013, 23:15

TỪ KHÓA LIÊN QUAN