30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

882 Extensible Markup Language (XML) Chapter 18<br />

Because XML allows document authors <strong>to</strong> create their own tags, naming collisions (i.e., two different<br />

elements that have the same name) can occur. As in <strong>Visual</strong> <strong>Basic</strong>, XML namespaces provide<br />

a means for document authors <strong>to</strong> prevent collisions.<br />

Each namespace prefix is bound <strong>to</strong> a uniform resource identifier (URI) that uniquely identifies the<br />

namespace. A URI is a series of characters that differentiate names. Document authors create their<br />

own namespace prefixes. Virtually any name can be used as a namespace prefix except the reserved<br />

namespace prefix xml.<br />

To eliminate the need <strong>to</strong> place a namespace prefix in each element, authors can specify a default<br />

namespace for an element and its children. We declare a default namespace using keyword xmlns<br />

with a URI (Universal Resource Indica<strong>to</strong>r) as its value.<br />

When an XML parser successfully parses a document, the parser s<strong>to</strong>res a tree structure containing<br />

the document’s data in memory. This hierarchical tree structure is called a Document Object Model<br />

(DOM) tree. The DOM tree represents each component of the XML document as a node in the tree.<br />

Nodes that contain other nodes (called child nodes) are called parent nodes. Nodes that have the same<br />

parent are called sibling nodes. A node’s descendant nodes include that node’s children, its children’s<br />

children and so on. A node’s ances<strong>to</strong>r nodes include that node’s parent, its parent’s parent and so on.<br />

The DOM tree has a single root node that contains all other nodes in the document.<br />

Namespace System.Xml, contains classes for creating, reading and manipulating XML documents.<br />

XmlReader derived class XmlNodeReader iterates through each node in the XML document.<br />

Class XmlReader is an MustInherit class that defines the interface for reading XML documents.<br />

A new XmlDocument object conceptually represents an empty XML document.<br />

The XML documents are parsed and loaded in<strong>to</strong> an XmlDocument object when method Load<br />

is invoked. Once an XML document is loaded in<strong>to</strong> an XmlDocument, its data can be read and<br />

manipulated programmatically.<br />

An XmlNodeReader allows us <strong>to</strong> read one node at a time from an XmlDocument.<br />

Method Read of XmlReader reads one node from the DOM tree.<br />

The Name property contains the node’s name, the Value property contains the node’s data and<br />

the NodeType property contains the node type (i.e., element, comment, text, etc.).<br />

Line breaks use the constant vbCrLf, which denotes a carriage return followed by a line feed.<br />

This is the standard line break for Windows-based applications and controls.<br />

Method CreateNode of XmlDocument takes a NodeType, a Name and a NamespaceURI<br />

as arguments.<br />

An XmlTextWriter streams XML data <strong>to</strong> disk. Method WriteTo writes an XML representation<br />

<strong>to</strong> an XmlTextWriter stream.<br />

An XmlTextReader reads XML data from a file.<br />

Class XPathNaviga<strong>to</strong>r in the System.Xml.XPath namespace can iterate through node<br />

lists that match search criteria, written as an XPath expression.<br />

XPath (XML Path Language) provides a syntax for locating, specific nodes in XML documents<br />

effectively and efficiently. XPath is a string-based language of expressions used by XML and<br />

many of its related technologies.<br />

Navigation methods of XPathNaviga<strong>to</strong>r are MoveToFirstChild, MoveToParent,<br />

MoveToNext and MoveToPrevious. Each method performs the action that its name implies.<br />

Method MoveToFirstChild moves <strong>to</strong> the first child of the node referenced by the XPath-<br />

Naviga<strong>to</strong>r, MoveToParent moves <strong>to</strong> the parent node of the node referenced by the XPath-

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!