Module 6: Manipulating Cached XML

94 236 0
Module 6: Manipulating Cached XML

Đ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

Contents Overview 1 Lesson: Introduction to the XML Document Object Model 2 Lesson: Navigating XML Using the DOM 14 Lab 6.1: Manipulating Cached XmlDocument Objects (Optional) 37 Lesson: Creating New Nodes 43 Lesson: Modifying and Deleting Nodes 58 Review 72 Lab 6.1: Manipulating Cached XmlDocument Objects 74 Course Evaluation 88 Module 6: Manipulating Cached 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 6: Manipulating Cached XML iii Instructor Notes After completing this module, students will be able to:  Open and save Extensible Markup Language (XML) documents by using the Document Object Model (DOM).  Locate XML by using the DOM.  Create nodes in the XML node tree.  Modify and delete nodes in the XML node tree. To teach this module, you need the following materials:  Microsoft ® PowerPoint ® file 2663A_06.ppt  Code Samples file 2663A_06_Code.htm To prepare to effectively teach this module:  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 viewing a code sample. This will close the browser window and return you to the PowerPoint presentation. Presentation: 90 minutes Lab: 105 minutes Required materials Preparation tasks Hyperlinked Code Examples iv Module 6: Manipulating Cached XML How to Teach This Module This section contains information that will help you to teach this module. Lesson: Introduction to the XML Document Object Model This section describes the instructional methods for teaching each topic in this lesson. Explain what the Document Object Model (DOM) is, and the typical parts of an XML document. Ask students if they have had any experience working with the DOM and, if so, to describe what they have done. Walk through the information on the slide and relate the XML document in the text to the nodes on the slide graphic. Relate the elements and attributes of the text to the nodes on the graphic. This topic consists of a large table, most of which is on the slide. As you go through the rows in the table, describe the purpose of each of the node types. Point out the classes that descend from XmlNode and explain that the descended classes represent what you find in an XML document. Take time to establish the relationships of the derived classes. This topic contains code samples that show how to load an XmlDocument object from a file, stream, reader, or from a string. This topic shows how use the Save() method of an XmlDocument object to save the contents of the object to a file. Lesson: Navigating XML Using the DOM This section describes the instructional methods for teaching each topic in this lesson. This demonstration shows how to navigate nodes in an XML document. Be sure to follow the navigation instructions exactly when you perform this demonstration. Explain how you can use an XmlNode object to refer to any of the descended objects in a DOM tree. Explain that when you have a node context, you can navigate around the document by using relative navigational properties such as FirstChild, NextSibling, and PreviousSibling. Explain how to use the SelectSingleNode() property of an XmlDocument object to provide random access to a single node in the tree. Contrast this random approach to the relative approach you covered in the previous topic. Explain how to use the XmlElement class to parse through an XmlDocument object in memory and to examine its element nodes. You use the GetAttribute() method of an XmlElement object to return the value of a specific parameter. What Is the XML Document Object Model? How Dom Nodes Correspond to XML DOM Nodes and Related .NET Node Types What .NET Classes Support the DOM? How to Load a DOM from an XML Source How to Save a DOM to a Document Demonstration: Navigating a Simple Document What Is the XmlNode Class? Using XmlNode Properties Referencing Individual Nodes in an XmlDocument Object How to Parse an XmlDocument Object in Memor y Module 6: Manipulating Cached XML v This topic describes how to populate an XmlNodeList object by using different data sources. Explain how to use a NamedNodeMap object to work with an unordered set of nodes. Follow the instructions carefully. Be sure to go through this code examination in detail before teaching this lesson. As you go through the code, relate each step to the previous topics in this lesson. Lesson: Creating New Nodes This section describes the instructional methods for teaching each topic in this lesson. Go through this demonstration carefully before teaching this lesson. This demonstration sets up the remaining topics in this lesson by showing how to add new nodes to an XmlDocument object in memory. Explain the general algorithm for adding new nodes. Explain the three methods that you use to add new nodes. This topic uses detailed code examples to show how to create specific types of nodes in an XML document that you have loaded into memory. You can set and create attributes in one or two steps. Contrast the two techniques for doing this. This topic describes how to create nodes that contain comments, CDATA information, and processing instructions. This section examines in detail the content of the demonstration that began this lesson. Be sure you are familiar with the code and what it does. Lesson: Modifying and Deleting Nodes This section describes the instructional methods for teaching each topic in this lesson. Contrast using the SelectSingleNode() method of an XmlNode object with using the GetElementsByTagName to identify individual nodes. Stress using the InnerText property to modify the contents of a node. Explain that you cannot use the Value property for this purpose because it is a read-only property. This topic consists essentially of detailed code examples that show how to use the RemoveChild() method of an XmlNode object to delete an element, and how to use the RemoveAttribute() method of an XmlElement object to delete an attribute. This practice reinforces the content of the topics that describe how to modify nodes. What Is the XmlNodeList Class? What Is the XmlNamedNodeMap Class? Code Examination: Navigating a Simple Document Demonstration: Adding Nodes to an XmlDocument Object How to add a Node to an XmlDocument Object How to Create an Element Node How to Set Attributes for an Element Node How to Create Other Types of Nodes Code Examination: Adding Nodes to an XmlDocument Ob ject How to Navigate to an Existing Node How to Modify a Node in an XmlDocument Object How to Delete a Node from an XmlDocument Object Practice: Modifying Nodes Module 6: Manipulating Cached XML 1 Overview  Introduction to the XML Document Object Model  Navigating XML Using the DOM  Creating New Nodes  Modifying and Deleting Nodes ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** This module introduces the XML Document Object Model (XML DOM) and explains how to locate and manipulate XML by using the DOM. After completing this module, you will be able to:  Open and save XML by using the DOM.  Locate XML by using the DOM.  Create nodes in the XML node tree.  Modify and delete nodes in the XML node tree. Introduction Objectives 2 Module 6: Manipulating Cached XML Lesson: Introduction to the XML Document Object Model  What Is the XML Document Object Model?  How DOM Nodes Correspond to XML  DOM Nodes and Related .NET Node Types  What .NET Classes Support the DOM?  How to Load a DOM from an XML Source  How to Save a DOM to a Document ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** This lesson gives an overview of the Document Object Model (DOM). You will use this information to manipulate XML in other lessons in this course. After completing this lesson, you will be able to:  Explain what the DOM is.  Build a DOM tree from an XML document.  Identify the .NET classes that support the DOM.  Load a DOM from an XML source.  Save a DOM to an XML document. Introduction Lesson objectives Module 6: Manipulating Cached XML 3 What Is the XML Document Object Model?  DOM is the W3C programming interface for XML  DOM models an XML source as a tree of nodes in memory  You can use DOM to:  Navigate and search  Add and delete content City.xml City.xml City.xml in memory City.xml in memory <city> <name elev="9M">Svolvaer</name> </city> <city> <name elev="9M">Svolvaer</name> </city> DocumentNode AttrNode Element Node Document Root <city> Element Node <name> elev=“9M” TextCharacterData Svolvaer XML Example ***************************** ILLEGAL FOR NON - TRAINER USE ****************************** The XML DOM provides a programming interface for applications to manipulate XML. The World Wide Web Consortium (W3C) specification for the DOM defines its properties, methods, and events. You can use the DOM with Microsoft ® XML Core Services 4 (MSXML4) or Microsoft Visual Studio ® .NET and the Microsoft .NET Framework classes to search and edit XML data. Both of these implementations fully support the W3C standard and provide additional features that make it easier for you to program applications to work with XML files. The main reason to use the DOM is to cache XML data in memory, as opposed to using an XmlReader or XmlWriter, which can only parse and generate non cached documents. You should use the DOM whenever you need to perform random-access style manipulation of XML data. After an XML document is loaded into memory, an application can use the DOM to:  Search for specific content.  Add, remove, or replace content.  Save the result to an XML file. Introduction Why use the DOM? 4 Module 6: Manipulating Cached XML An application can use the DOM methods to create a representation in memory of an XML source document. The DOM represents XML in memory as a tree of node types. For example, the DOM maps the document element to a node called DocumentNode and sets this value to the name of the document element. You should use the DOM whenever you need to perform in-memory, random access style modification of XML data. Consider the following XML document, which represents a purchase order. It might have several <date> elements, each appearing in a different part of the document: <order> <date>02.01.03</date> <item>canvas</item> <reorder> <date>02.07.04</date> <item>linen</item> </reorder> </order> An application that uses the DOM can recognize specific node types, such as the document element and content, and then respond appropriately. For example, the application can locate and update the occurrence of the <date> element nested within the <reorder> element. The DOM is not well suited to processing large XML files. Converting a large XML data source into memory can cause some systems to slow down dramatically. You might not need to load an XML document into memory to process it. Consider the following scenarios. To read and parse data from an XML document, you should use a strategy that employs forward-only reading and parsing, without loading the document into memory. In the .NET Framework, you can use an XmlTextReader or XmlValidatingReader. To query specific information from an XML document, you should use a strategy optimized for querying non-cached data. In the .NET Framework, you can use an XPathDocument object. The DOM is not well suited to changing XML structure. If you want to convert an XML document into another vocabulary, you use the Extensible Stylesheet Language for Transformation (XSLT) instead of the DOM. In the .NET Framework, you can use an XslTransform object. When to use the DOM Limitations of the DOM Readin g XML Querying XML Transforming XML [...]... < ?xml version="1.0"?> XmlSignificantWhitespace An XML significant white space XmlWhiteSpace An XML white space EndElement Returned by an XmlReader when reaching an end element; for example, EndEntry An end entry Module 6: Manipulating Cached XML 9 What NET Classes Support the DOM? NET Framework DOM class hierarchy Object XmlNode XmlLinkedNode XmlCharacterData XmlDeclaration XmlDocumentType XmlElement... Object XmlNode XmlLinkedNode XmlCharacterData XmlDeclaration XmlDocumentType XmlElement XmlEntityRef XmlProcessingInstruction XmlAttribute XmlDocument XmlDocumentFragment XmlDocumentType XmlEntity XmlNotation XmlNamedNodeMap XmlAttributeCollection XmlNodeList XmlCDATASection XmlComment XmlSignificantWhiteSpace XmlWhiteSpace *****************************ILLEGAL FOR NON-TRAINER USE******************************... 'Visual Basic Imports System .Xml Dim doc As New XmlDocument() // C# Using System .Xml; XmlDocument doc = new XmlDocument(); Module 6: Manipulating Cached XML Loading an XmlDocument from a file, stream, or reader You can load an XmlDocument with data from a file, stream, XmlReader, or TextReader by using the Load() method of the XmlDocument object The following example creates a new XmlDocument object and... individual nodes in an XmlDocument object Parse an XmlDocument object in memory by using XML elements Describe the XmlNodeList class Describe the XmlNamedNodeMap class Load XML by using the DOM Module 6: Manipulating Cached XML 15 Demonstration: Navigating a Simple Document A Windows Application navigates through an XML document Notice how it loads XML data into memory by using an XmlDocument object Notice... XmlNode = doc.DocumentElement Dim NodePointer As IEnumerator = root.GetEnumerator() Dim book As XmlNode While NodePointer.MoveNext() book = CType(NodePointer.Current, XmlNode) MessageBox.Show(book.OuterXml) End While Module 6: Manipulating Cached XML 19 Using XmlNode Properties XmlElement and XmlAttribute objects are also nodes To access content, use properties and methods < ?xml version="1.0"?> < ?xml. .. the first book element containing a last-name element with a value of Austen Module 6: Manipulating Cached XML 17 What Is the XmlNode Class? Used to handle XML by its node characteristics Acts as a pointer to nodes when navigating a document Base class for XmlDocument XmlDocumentFragment XmlEntity XmlLinkedNode XmlAttribute XmlNotation Visual Basic Example C# Example *****************************ILLEGAL... //Create the XmlDocument XmlDocument doc = new XmlDocument(); doc.Load("C:\\BookData .xml" ); 'Visual Basic 'Create the XmlDocument Dim doc As XmlDocument = New XmlDocument() doc.Load("C:\\BookData .xml" ) Loading an XmlDocument from a string You can load an XmlDocument object with data from a string by using the LoadXml() method of the XmlDocument object The following example creates a new XmlDocument... MessageBox.Show(root.LastChild.OuterXml); 'Visual Basic Dim doc As XmlDocument = New XmlDocument() doc.LoadXml("" + _ "Pride And Prejudice" + _ "19.95" + _ "") Dim root As XmlNode = doc.FirstChild 'Display the price element MessageBox.Show(root.LastChild.OuterXml) 25 26 Module 6: Manipulating Cached XML How to Parse an XmlDocument Object in Memory Use the XmlElement... data as XML MyXmlDocument.Save(UpdatedDocument) Catch e As Exception MessageBox.Show(e.ToString()) End Try 14 Module 6: Manipulating Cached XML Lesson: Navigating XML Using the DOM Demonstration: Navigating a Simple Document What Is the XmlNode Class? Using XmlNode Properties Referencing Individual Nodes in an XmlDocument Object How to Parse an XmlDocument Object in Memory What Is the XmlNodeList Class?... Types W3C DOM node types Related NET DOM node type Document XmlDocument DocumentFragment XmlDocumentFragment DocumentType XmlDocumentType EntityReference XmlEntityReference Element XmlElement Attr XmlAttribute ProcessingInstruction XmlProcessingInstruction Comment XmlComment Text XmlText CDATASection XmlCDATASection Entity XmlEntity Notation XmlNotation *****************************ILLEGAL FOR NON-TRAINER . XmlCharacterData XmlDeclaration XmlDocumentType XmlElement XmlEntityRef XmlProcessingInstruction XmlAttribute XmlDocument XmlDocumentFragment XmlDocumentType XmlEntity. How XML is represented in a DOM object Module 6: Manipulating Cached XML 7 DOM Nodes and Related .NET Node Types XmlNotationNotation XmlEntityEntity XmlCDATASectionCDATASection

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

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan