Accessing an XML File

Team-Fly    

Developing XML Web Services and Server Components with Visual C#™ .NET and the .NET Framework, Exam Cram™ 2 (Exam 70-320)
By Amit Kalani, Priti Kalani

Table of Contents
Chapter 3.  Accessing and Manipulating XML Data


In this section, you'll learn how to extract information from an XML file. I'll start by showing you how you can use the XmlReader object to move through an XML file, extracting information as you go. Then you'll see how other objects, including the XmlNode and XmlDocument objects, provide a more structured view of an XML file.

I'll work with a very simple XML file named Books.xml that represents three books that a computer bookstore might stock. Here's the raw XML file:

 <?xml version="1.0" encoding="UTF-8"?> <Books>     <Book Pages="1200">        <Author>Kalani, Amit</Author>        <Title>Exam 70-316 Training Guide</Title>        <Publisher>Que Certifications</Publisher>     </Book>     <Book Pages="997">        <Author>Burton, Kevin</Author>        <Title>.NET Common Language Runtime</Title>        <Publisher>Sams</Publisher>     </Book>     <Book Pages="392">        <Author>Cooper, James W.</Author>        <Title>C# Design Patterns</Title>        <Publisher>Addison-Wesley</Publisher>     </Book> </Books> 

Understanding the DOM

The Document Object Model, or DOM, is an Internet standard for representing the information contained in an HTML or XML document as a tree of nodes. Like many other Internet standards, the DOM is an official standard of the World Wide Web Consortium, better known as the W3C.

In its simplest form, the DOM defines an XML document as a tree of nodes. The root element in the XML file becomes the root node of the tree, and other elements become child nodes. The DOM provides the standard for constructing this tree, including a classification for individual nodes and rules for which nodes can have children.

graphics/alert_icon.gif

In the DOM, attributes are not represented as nodes within the tree. Instead, attributes are considered to be properties of their parent elements.


Using an XmlReader Object

The XmlReader class is designed to provide forward-only, read-only access to an XML file. This class treats an XML file similarly to the way that a cursor treats a result set from a database. At any given time, there is one current node within the XML file, represented by a pointer that you can move around within the file. The class implements a Read() method that returns the next XML node to the calling application. There are also many other members in the XmlReader class; I've listed some of these in Table 3.1.

Table 3.1. Important Members of the XmlReader Class

Member

Type

Description

Depth

Property

Specifies the depth of the current node in the XML document

EOF

Property

Represents a Boolean property that is true when the current node pointer is at the end of the XML file

GetAttribute()

Method

Gets the value of an attribute

HasAttributes

Property

Returns true when the current node contains attributes

HasValue

Property

Returns true when the current node can have a Value property

IsEmptyElement

Property

Returns true when the current node represents an empty XML element

IsStartElement()

Method

Determines whether the current node is a start tag

MoveToElement()

Method

Moves to the element containing the current attribute

MoveToFirstAttribute()

Method

Moves to the first attribute of the current element

MoveToNextAttribute()

Method

Moves to the next attribute

Name

Property

Specifies a qualified name of the current node

NodeType

Property

Specifies the type of the current node

Read()

Method

Reads the next node from the XML file

Skip()

Method

Skips the children of the current element

Value

Property

Specifies the value of the current node

The XmlReader class is a purely abstract class. You cannot create an instance of XmlReader in your own application. Generally, you'll use the XmlTextReader class instead. The XmlTextReader class implements XmlReader for use with text streams. Take the following steps to learn how to use the XmlTextReader class.

  1. Create a new blank solution named C03 at C:\EC70320. Add a new Visual C# Windows application project (Example3_1) to the solution. Add the Books.xml file (discussed in the previous section) to the project.

  2. Add a Button control (btnReadXml) and a ListBox control (lbNodes) to the form. Switch to Code view and add the following using directives:

     using System.Xml; using System.Text; 
  3. Double-click the Button control and add the following code to handle the button's Click event:

     private void btnReadXML_Click(object sender, System.EventArgs e) {     StringBuilder sbNode = new StringBuilder();     // Create a new XmlTextReader on the file     XmlTextReader xtr = new XmlTextReader(@"..\..\Books.xml");     // Walk through the entire XML file     while(xtr.Read())     {         sbNode.Length = 0;         for(int intI=1; intI <= xtr.Depth ; intI++)             sbNode.Append(" ");         sbNode.Append(xtr.Name + " ");         sbNode.Append(xtr.NodeType.ToString());         if (xtr.HasValue)             sbNode.Append(": " + xtr.Value);         lbNodes.Items.Add(sbNode.ToString());         // Now add the attributes, if any         if (xtr.HasAttributes)             while(xtr.MoveToNextAttribute())             {                 sbNode.Length=0;                 for(int intI=1;intI <= xtr.Depth;intI++)                     sbNode.Append(" ");                 sbNode.Append(xtr.Name + " ");                 sbNode.Append(xtr.NodeType.ToString());                 if (xtr.HasValue)                     sbNode.Append(": " + xtr.Value);                 lbNodes.Items.Add(sbNode.ToString());             }     }     // Clean up     xtr.Close(); } 
  4. Build and run the project. Click the button. You'll see a schematic representation of the XML file in the listbox.

The output includes nodes for everything in the XML file, including the XML declaration and any whitespace (such as the line feeds and carriage returns that separate lines of the files).

Note the use of the NodeType property and the MoveToNextAttribute() method in this example to display the elements, text nodes, and attributes from the file. Although XmlTextReader does not consider attributes to be nodes, it provides the MoveToNextAtttibute() method to treat them as nodes.

graphics/alert_icon.gif

An alternate way of retrieving attributes is to use indexers on the XmlTextReader object. If the current node represents an element in the XML file, this code retrieves the value of the first attribute of the element:

 xtr[0] 

This code retrieves the value of an attribute named Pages:

 xtr["Pages"] 


The XmlNode Class

The code you saw in the previous example deals with nodes as part of a stream of information returned by the XmlTextReader object. But the .NET Framework also includes another class, XmlNode, which can be used to represent an individual node from the DOM representation of an XML document. If you instantiate an XmlNode object to represent a particular portion of an XML document, you can alter the properties of the object and then write the changes back to the original file. The DOM provides two-way access to the underlying XML in this case.

graphics/note_icon.gif

In addition to XmlNode, the System.Xml namespace contains a set of classes that represent particular types of nodes: XmlAttribute, XmlComment, XmlElement, and so on. These classes all inherit from the XmlNode class.


The XmlNode class has a rich interface of properties and methods. You can retrieve or set information about the entity represented by an XmlNode object, or you can use its methods to navigate the DOM. Table 3.2 shows the important members of the XmlNode class.

Table 3.2. Important Members of the XmlNode Class

Member

Type

Description

AppendChild()

Method

Adds a new child node to the end of this node's list of children

Attributes

Property

Returns the attributes of the node as an XmlAttributeCollection object

ChildNodes

Property

Returns all child nodes of this node

CloneNode()

Method

Creates a duplicate of this node

FirstChild

Property

Returns the first child node of this node

HasChildNodes

Property

Returns true if this node has any children

InnerText

Property

Specifies the value of the node and all its children

InnerXml

Property

Specifies the markup representing only the children of this node

InsertAfter()

Method

Inserts a new node after this node

InsertBefore()

Method

Inserts a new node before this node

LastChild

Property

Returns the last child node of this node

Name

Property

Specifies the node's name

NextSibling

Property

Returns the next child of this node's parent node

NodeType

Property

Specifies this node's type

OuterXml

Property

Specifies the markup representing this node and its children

OwnerDocument

Property

Specifies the XmlDocument object that contains this node

ParentNode

Property

Returns this node's parent

PrependChild()

Method

Adds a new child node to the beginning of this node's list of children

PreviousSibling

Property

Returns the previous child of this node's parent node

RemoveAll()

Method

Removes all children of this node

RemoveChild()

Method

Removes a specified child of this node

ReplaceChild()

Method

Replaces a child of this node with a new node

SelectNodes()

Method

Selects a group of nodes matching an XPath expression

SelectSingleNode()

Method

Selects the first node matching an XPath expression

WriteContentTo()

Method

Writes all children of this node to an XmlWriter object

WriteTo()

Method

Writes this node to an XmlWriter object

The XmlDocument Class

There's no direct way to create an XmlNode object that represents an entity from a particular XML document. Instead, you can retrieve XmlNode objects from an XmlDocument object. The XmlDocument object represents an entire XML document. Take the following steps to learn how to use the XmlNode and XmlDocument objects to navigate through the DOM representation of an XML document.

  1. Add a new Windows application project (Example3_2) to the solution. Add the Books.xml file to the project.

  2. Add a Button control (btnReadXml) and a ListBox control (lbNodes) to the form. Switch to Code view and add the following using directives:

     using System.Xml; using System.Text; 
  3. Double-click the button and add the following code to handle the button's Click event:

     private void btnReadXML_Click(object sender, System.EventArgs e) {         // Create a new XmlTextReader on the file     XmlTextReader xtr = new XmlTextReader(@"..\..\Books.xml");     // Load the XML file to an XmlDocument     xtr.WhitespaceHandling = WhitespaceHandling.None;     XmlDocument xd = new XmlDocument();     xd.Load(xtr);     // Get the document root     XmlNode xnodRoot = xd.DocumentElement;     // Walk the tree and display it     XmlNode xnodWorking;     if (xnodRoot.HasChildNodes)     {         xnodWorking = xnodRoot.FirstChild;         while (xnodWorking != null)         {             AddChildren(xnodWorking, 0);             xnodWorking = xnodWorking.NextSibling;         }     }     xtr.Close(); } private void AddChildren(XmlNode xnod, Int32 intDepth) {     // Adds a node to the ListBox, together with its children     StringBuilder sbNode = new StringBuilder();     // Process only Text and Element nodes     if((xnod.NodeType == XmlNodeType.Element) ||         (xnod.NodeType == XmlNodeType.Text))     {         sbNode.Length = 0;         for(int intI=1; intI <= intDepth ; intI++)             sbNode.Append(" ");         sbNode.Append(xnod.Name + " ");         sbNode.Append(xnod.NodeType.ToString());         sbNode.Append(": " + xnod.Value);         lbNodes.Items.Add(sbNode.ToString());         // Now add the attributes, if any         XmlAttributeCollection atts = xnod.Attributes;         if(atts != null)             for(int intI = 0; intI < atts.Count; intI++)             {                 sbNode.Length = 0;                 for (int intJ = 1; intJ <= intDepth + 1; intJ++)                     sbNode.Append(" ");                 sbNode.Append(atts[intI].Name + " ");                 sbNode.Append(atts[intI].NodeType.ToString());                 sbNode.Append(": " + atts[intI].Value);                 lbNodes.Items.Add(sbNode);             }         // And recursively walk the children of this node         XmlNode xnodworking;         if (xnod.HasChildNodes)         {             xnodworking = xnod.FirstChild;             while (xnodworking != null)             {                 AddChildren(xnodworking, intDepth + 1);                 xnodworking = xnodworking.NextSibling;             }         }     } } 
  4. Build and run the project. Click the button. You'll see a schematic representation of the elements and attributes in the XML file.

The previous example uses recursion to visit all the nodes in the XML file. That is, it starts at the document's root node (returned by the DocumentElement property of the XmlDocument object) and visits each child of that node in turn. For each child, it displays the desired information, then visits each child of that node in turn, and so on.

Table 3.3 lists the most important members of the XmlDocument class. The XmlDocument class is a subclass of the XmlNode class; therefore, in addition to the members listed in Table 3.3, it also has the members of the XmlNode class.

Table 3.3. Important Members of the XmlDocument Class

Member

Type

Description

CreateAttribute()

Method

Creates an attribute node

CreateElement()

Method

Creates an element node

CreateNode()

Method

Creates an XmlNode object

DocumentElement

Property

Returns the root XmlNode object for this document

DocumentType

Property

Returns the node containing the DTD declaration for this document, if it has one

ImportNode()

Method

Imports a node from another XML document

Load()

Method

Loads an XML document into the XmlDocument object

LoadXml()

Method

Loads the XmlDocument object from a string of XML data

NodeChanged

Event

Occurs after the value of a node has been changed

NodeChanging

Event

Occurs when the value of a node is about to be changed

NodeInserted

Event

Occurs when a new node has been inserted

NodeInserting

Event

Occurs when a new node is about to be inserted

NodeRemoved

Event

Occurs when a node has been removed

NodeRemoving

Event

Occurs when a node is about to be removed

PreserveWhitespace

Property

Returns true if whitespace in the document should be preserved when loading or saving the XML

Save()

Method

Saves the XmlDocument object as a file or stream

WriteTo()

Method

Saves the XmlDocument object to an XmlWriter object


    Team-Fly    
    Top


    MCAD Developing XML Web Services and Server Components with Visual C#. NET and the. NET Framework Exam Cram 2 (Exam Cram 70-320)
    Managing Globally with Information Technology
    ISBN: 789728974
    EAN: 2147483647
    Year: 2002
    Pages: 179

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net