Contents
Overview 1
Lesson: How Does DOM Work? 2
Lesson: Using DOM and the .NET
Framework XML Classes 8
Lab 5: Changing a DOM Tree 16
Review 26
Module 5: Creating and
Manipulating Trees
Using DOM
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.
!
Load an XML document into memory.
!
Navigate a tree to find data.
!
Retrieve and set node properties.
!
Save a DOM tree to an XML file.
Materials and Preparation
This section provides the materials and preparation tasks that you need to teach
this module.
To teach this module, you need the following materials:
!
Microsoft PowerPoint
®
file 2500A_05.ppt
!
XML Tree Viewer
To prepare for this module:
!
Read all of the materials for this module and complete the labs.
!
Read about DOM in the .NET Framework software development kit (SDK).
!
Read the W3C materials about DOM so that you can comfortably show
them to the class and comment on them briefly.
!
Develop as thorough an understanding as you can about the way DOM
Framework XML classes enable developers to use and extend the W3C
XML DOM by using base and extended classes. Direct participants to the
shortcut that Visual Studio .NET installs on the Desktop that opens the
Microsoft .NET Framework SDK. Emphasize the importance of using the
SDK when developing with the .NET Framework XML classes.
!
Using DOM and the .NET Framework XML Classes
This lesson builds a basic DOM solution by using the .NET Framework
XML classes. This lesson first describes how to load an XML document or
string. Then, it describes how to search and manipulate the data. Finally, it
describes how to save the result as a new XML file.
Module 5: Creating and Manipulating Trees Using DOM 1 Overview
!
How Does DOM Work?
!
Using DOM and the .NET Framework XML Classes
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
The Document Object Model (DOM) is an important technology to know when
working with XML documents. This module introduces DOM and how it is
enabled in the Microsoft
®
ILLEGAL FOR NON
-
TRAINER USE
******************************
After completing this lesson, you will be able to:
!
Describe how a DOM application works.
!
Describe how the .NET Framework XML Classes enable you to create a
DOM application.
Lesson objectives
Module 5: Creating and Manipulating Trees Using DOM 3 How Does a DOM Application Work?
Application
Application
DOM
"Load XML into a DOM object"
"Load XML into a DOM object"
XML Processor
XML
Result
"Search the DOM memory structure"
"Search the DOM memory structure"
Doc element
Element
Element
Attribute
application can locate a node according to some criterion that you specify,
such as by node type or node content. When the application locates a node
or set of nodes, it can perform some processing, such as change the node
content or add nodes.
4. The application can save the result as a new XML file or transmit the XML
result to some other software module for further processing.
Introduction
How a typical DOM
application processes
XML
4 Module 5: Creating and Manipulating Trees Using DOM An XML document is only one of many possible sources of XML. An XML
source can be a section of XML data that is contained within a:
!
String variable.
!
Text document.
!
Hypertext Markup Language (HTML) file.
!
Microsoft SQL Server
™
2000 database table.
DOM provides a set of techniques for modifying the document tree. You can
convert an original document that has been loaded into memory into some other
tree structure that suits a different purpose.
XmlAttribute -- represents an attribute
XmlComment -- represents a comment
Many others
*****************************
ILLEGAL FOR NON
-
TRAINER USE
******************************
The World Wide Web Consortium (W3C) developed DOM in levels. DOM
Level 1 and Level 2 have reached Recommendation status. A third level is in
the Working Draft stage of development.
The Microsoft .NET Framework software development kit (SDK) fully
supports and extends the classes described in the W3C DOM Level 1 and
Level 2 Recommendations. These extensions to DOM make it easier to work
with XML data. For example, several of the new classes enable you to access
relational data. They give you methods for synchronizing with ADO.NET data
and simultaneously exposing data as XML.
If you want to use Microsoft Visual Studio
®
.NET to write a program that uses
DOM, you will most likely use the following XML DOM base classes.
Use the members of this class: To represent:
XmlNode A single node in the document tree. It is the base
class for accessing data in the XML object model.
The XmlNodeType enumeration defines valid
node types.
XmlNodeList An ordered collection of nodes. It supports
iteration and indexed access methods on the live
XmlCharacterData Provides methods for text manipulation that are used
by other classes, such as XmlText and
XmlCDataSection.
XmlComment Represents the content of an XML comment.
XmlDeclaration Represents the XML declaration nodes,
<?xml version='1.0' … ?>.
XmlDocumentFragment Represents a fragment or portion of a document’s tree.
This is useful for tree insert operations.
XmlDocumentType Contains information associated with the document
type declaration.
XmlElement Represents an element object.
XmlEntity Represents a parsed or unparsed entity in the XML
document.
XmlEntityReference Represents an entity reference node.
XmlImplementation Provides methods for performing operations that are
independent of any particular instance of DOM.
XmlLinkedNode Gets the node immediately preceding or following this
node. Nodes as the instance of this class or classes
derived from it can be linked inside the DOM tree and
their proceeding or following nodes can be retrieved.
XmlNotation Contains a <!NOTATION declared in the DTD or
schema.
XML DOM extended
classes
Module 5: Creating and Manipulating Trees Using DOM 7 (continued)
Class Description
******************************
In this lesson you will learn how to build an application that uses the .NET
Framework XML classes that use DOM functionality.
After completing this lesson, you will be able to:
!
Load an XML source into memory as a DOM node tree.
!
Navigate a DOM node tree.
!
Retrieve and manipulate the data and the properties of DOM nodes.
!
Save a DOM node tree as an XML file. If you have not yet looked at the .NET Framework and .NET languages,
do not be too concerned. However, it is important to know that Microsoft
Visual Basic
®
.NET is very different from Visual Basic version 6.0 and is
effectively a new language. For more information about Visual Basic .NET, see
Course 2559A, Introduction to Visual Basic .NET Programming.
Introduction
Lesson objectives
Note