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

Module 4: Writing XML

52 422 0
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 52
Dung lượng 0,93 MB

Nội dung

Contents Overview 1 Lesson: Overview of Generating XML Documents 2 Lesson: Writing XML 6 Lesson: Generating XML with Namespaces 25 Lesson: Controlling XML Format and Converting XML 32 Review 41 Lab 4.1: Writing XML 43 Module 4: Writing 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 4: Writing XML iii Instructor Notes After completing this module, students will be able to:  Use the XmlTextWriter class to write well-formed XML.  Generate namespaces for elements and attributes.  Control indentation and other code format characteristics when generating XML.  Write XML that preserves characters that are normally represented by using escape characters. To teach this module, you need the following materials:  Microsoft ® PowerPoint ® file 2663A_04.ppt  Code samples file 2663A_04_Code.htm To prepare to effectively teach this module:  Read the following Microsoft Visual Studio ® Help topics: • Writing XML with the XmlWriter • XmlWriter Class • Namespace Features within the XmlTextWriter • XmlConvert Class  Read the entire module.  Complete the practices and the lab.  Practice delivering the demonstrations. In this module, some of the 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 you view a code sample. This will close the browser window and return you to the PowerPoint presentation. Presentation: 45 minutes Lab: 45 minutes Required materials Preparation tasks Hyperlinked Code Examples iv Module 4: Writing XML How to Teach This Module This section contains information that will help you to teach this module. Lesson: Overview of Generating XML Documents This section describes the instructional methods for teaching each topic in this lesson. Briefly discuss why an application might need to generate data in general and why it might be advantageous to generate that data in XML. Briefly describe how XmlTextWriter is a descendent of XmlWriter. The slide for the topic is a process illustration. Here is one way to talk about this illustration: 1. First, describe how data might be made available to an application by means of a connection to an OLE DB source or a file, such as a comma-separated value (CSV) file. If the data originates from an OLE DB source, it will likely be processed by using Microsoft ADO.NET classes. If the data originates from a file format, the application will likely use a stream object as an intermediary. 2. Next, you might discover that the XML that is being sent to your application contains XML that is not well-formed. For example, a database application might send XML elements that contain spaces, because the table names upon which the XML is based also contain spaces. In such cases, you can use the XmlConvert() method to process known problems in incoming XML. 3. Now, you can write the XML by using XmlTextWriter. Lesson: Writing XML This section describes the instructional methods for teaching each topic in this lesson. This demonstration is followed later in the lesson by a code examination. Briefly show that the application writes XML. Show the code that creates an XmlTextWriter. Show the code that writes the XML document root. Show the code that writes elements. Show the code that writes attributes. Show the code that writes processing instructions, comments, and CDATA sections. Why Generate XML? What Is the XmlTextWriter Class? The Process of Writing XML Demonstration: Writing XML How to Create an XmlTextWriter Ob ject How to Write the XML Document Root How to Write Elements How to Write Attributes How to Write Processing Instructions, Comments, and CDATA Module 4: Writing XML v Use the debugging environment to show how the code modifies several objects. This practice allows students to write the XML parts described in the preceding topics. Provide learners with plenty of time to complete this practice. Offer plenty of guidance, because the skills practiced here are fundamental. Lesson: Generating XML With Namespaces This section describes the instructional methods for teaching each topic in this lesson. Briefly summarize the main benefits of using XML namespaces and the technical problems they are used to solve. Learners should already know this information, so this topic is a review. Show the code that generates elements with namespaces. Show the code that generates attributes with namespaces. Lesson: Controlling XML Format and Converting XML This section describes the instructional methods for teaching each topic in this lesson. Show the code that sets XML formatting, such as how to indent the XML and whether a single or a double quotation character is used. Show the code that controls aspects of generated XML, such as white space and the use of escape characters in element names. Show the code that invokes XmlConvert. The main use for this class is to programmatically fix errors in XML names. You can also use it to convert strongly typed data into a string type that is suitable for generating XML content. Use the custom add-in and watch expressions to show how code converts an OLE DB data table source to XML. Code Examination: Writing XML from a Text File Practice: Writing XML Content and Nodes Why Create Components with Namespaces? How to Generate Elements with Namespaces How to Generate Attributes with Namespaces How to Set XML Formattin g How to Control Text Aspects of XML How to Convert XML Code Examination: Writing XML from a Database vi Module 4: Writing XML 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 Training and Certification 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. Module 4: Writing XML 1 Overview  Overview of Generating XML Documents  Writing XML  Generating XML with Namespaces  Controlling XML Format and Converting XML ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** Writing Extensible Markup Language (XML) is a task that is central to developing XML-based applications. In this chapter, you will learn about the Microsoft ® .NET Framework classes that you use to generate XML. You will also learn how to incorporate namespaces as the XML is generated. Finally, you will learn how to control many format characteristics of generated XML. After completing this module, you will be able to:  Use the XmlTextWriter class to write well-formed XML.  Generate namespaces for elements and attributes.  Control indentation and other code format characteristics when generating XML.  Write XML that preserves characters that are normally represented by using escape characters. Introduction Objectives 2 Module 4: Writing XML Lesson: Overview of Generating XML Documents  Why Generate XML?  What Is the XmlTextWriter Class?  The Process of Writing XML ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The XmlTextWriter class is an implementation of the XmlWriter class that provides the application programming interface (API) that writes XML to a file, stream, or TextWriter. Typically, you use the XmlTextWriter class to write XML as raw data. Although you can accomplish this task by using the Document Object Model (DOM), XmlTextWriter is often preferred because it is more efficient. After completing this lesson, you will be able to:  Identify development tasks requiring XML writing functionality.  Identify the places in the process of writing XML that can be handled by using the XmlTextWriter class. Introduction Lesson objectives Module 4: Writing XML 3 Why Generate XML?  To translate data formats into XML  SQL Server database tables  OLE DB data sources  Electronic Data Interchange (EDI) ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The main reason to generate XML is to encode data in a format that can be easily decoded, transformed, and shared. Often the reason to generate XML is to translate data that is formatted in a language or system other than XML to prepare the data for use in an application that can use XML. Data formats that might serve as sources for the data from which you generate XML include:  Microsoft SQL Server ™ database tables  OLE DB data sources  Electronic data interchange (EDI) Another reason to generate XML is to encode application data, such as the data for storing session state and user preferences. For example, if you search a computer running Microsoft Windows ® XP or Microsoft Office XP for XML files, you will build a list of files used for data storage by applications. Introduction Translating data into XML Storin g application data 4 Module 4: Writing XML What Is the XmlTextWriter Class?  Writes XML rapidly  Non-cached  Forward-only XmlTextWriter derived from XmlWriter XmlWriter An abstract base class <a> <b></b> </a> ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** XmlWriter is an abstract base class that defines an interface for writing XML. The XmlWriter class provides a forward-only, read-only, non-cached way of generating XML streams. This helps you to build XML documents that conform to the World Wide Web Consortium (W3C) XML 1.0 (Second Edition) recommendation, located at http://www.w3.org/TR/2000/REC-xml-20001006.html, and the W3C Namespaces in XML recommendation, located at http://www.w3.org/TR/REC-xml-names/. XmlTextWriter class is derived from XmlWriter. In your application you will use the XmlTextWriter to implement the functionality of XmlWriter. The XmlTextWriter class has methods and properties that are defined to:  Specify whether to support namespaces.  Write well-formed XML.  Encode binary bytes as base64 and as BinHex, and write out the resulting text.  Manage the output, including methods to determine the progress of the output, by using the WriteState property.  Write multiple documents to one output stream.  Flush or close the output.  Write valid names, qualified names, and name tokens. Introduction What the XmlTextWriter can do [...]... the XmlWriter class does not process the Unicode characters into escape characters The existence of duplicate attributes To prevent XML errors from propagating through the writing process, you would use XmlConvert to fix XML error prior to writing by using XmlTextWriter 6 Module 4: Writing XML Lesson: Writing XML Demonstration: Writing XML How to Create an XmlTextWriter Object How to Write the XML. .. sections Module 4: Writing XML 7 Demonstration: Writing XML The XML Tools add-in shows how to write XML: From plain text, such as a commaseparated value (CSV) file From an OLE DB data source, such as a Microsoft Access table *****************************ILLEGAL FOR NON-TRAINER USE****************************** Introduction Demonstration This demonstration introduces the XML writing functionality of the XML. .. text file into XML 1 In Microsoft Visual Studio® NET, open the file names.csv 2 On the XML Tools toolbar, click Text to XML 3 In the Write XML from Text File dialog box, click OK The resulting XML file appears in the Output window Notice that the columns are stored as attributes and are named Column01, Column02, and so on 8 Module 4: Writing XML 4 On the XML Tools toolbar, click Text to XML Note If the.. .Module 4: Writing XML 5 The Process of Writing XML Product Name S K U Price coffee mug 11234 7.99 tea infuser 45256 3.99 CSV file OLE DB ADO.NET Stream XmlConvert object fix any errors of form in the XML XmlTextWriter write XML Before Conversion After Conversion ... a TODO: Mod 04: instantiate a writer using a memory stream b TODO: Mod 04: reset column counter c TODO: Mod 04: write an attribute otherwise write an element 5 On the Debug menu, click Start A second instance of Visual Studio NET starts 20 Module 4: Writing XML To convert a sample text file into XML 1 Open the file names.csv for this demonstration 2 On the XML Tools toolbar, click Text to XML 3 Click... is not used, an XML fragment is created and verifies whether the document is well-formed Root-level rules are not applied Module 4: Writing XML Root element 13 Because a well-formed XML document must have a root element, you normally combine a WriteStartDocument() method call with a WriteStartElement() method call In this example, an XmlTextWriter is created It writes XML to books .xml and uses UTF-8... BookWriter.WriteEndElement(); XML Output > Module 4: Writing XML 17 How to Write Processing Instructions, Comments, and CDATA Writing a processing instruction BookWriter.WriteProcessingInstruction( BookWriter.WriteProcessingInstruction( "xml- stylesheet", "type='text/xsl' href='style.xsl'"); "xml- stylesheet", "type='text/xsl' href='style.xsl'"); Writing a comment BookWriter.WriteComment("This... Output window 9 10 Module 4: Writing XML How to Create an XmlTextWriter Object To use the XmlTextWriter constructor set the following: Parameter to a TextWriter, Stream, or String Encoding property to ASCII, UTF-7, UTF-8 or Unicode Formatting property to Indented ' Visual Basic ' Visual Basic Dim BookWriter As New XmlTextWriter( _ Dim BookWriter As New XmlTextWriter( _ "\catalog\books .xml" , Encoding.UTF7)... attributes that contain namespace references 26 Module 4: Writing XML Why Create Components with Namespaces? Namespaces provide a way to mark elements as belonging to one group or another Use namespaces to avoid naming conflicts when merging data from multiple XML sources Jane Doe... name and text, as follows: ' Visual Basic BookWriter.WriteProcessingInstruction( "xml- stylesheet", _ "type='text/xsl' href='books.xsl'") // C# BookWriter.WriteProcessingInstruction( "xml- stylesheet", "type='text/xsl' href='books.xsl'"); XML Output > < ?xml- stylesheet type='text/xsl' href='books.xsl' ?> 18 Module 4: Writing XML How to write a comment The WriteComment() method writes a comment containing . Introduction What the XmlTextWriter can do Module 4: Writing XML 5 The Process of Writing XML XmlTextWriter write XML CSV file ADO.NET XmlConvert object fix. the writing process, you would use XmlConvert to fix XML error prior to writing by using XmlTextWriter. Introduction 6 Module 4: Writing XML Lesson: Writing

Ngày đăng: 18/10/2013, 17:15

TỪ KHÓA LIÊN QUAN