Site icon IT World Canada

.Net and XML interwoven

Talking Shop

Having recently returned from a technology conference in New Jersey, I’ve been rather immersed in .Net. It was surprising to see how many of the business units in my organization have already committed to using .Net as their primary development platform. What’s not surprising, however, is that most of them made this decision in light of the fact that Microsoft isn’t giving them much choice. With .Net, Microsoft has put all its eggs in one basket, with plans to wean its customers from classic ASP and Visual Basic.

Actually, I should say that there are two baskets: .Net architecture and XML. But these baskets are so woven together it’s nearly impossible to think of the former without the latter.

Let’s take a look at the advanced XML handling that C# .Net provides, using the following XML file as our example:

Marc

2 St. Andrews

555-1234

Gillian

35 Aurora St.

555-9876

Reading an XML Document

Code Block 1 (see below) is an example of using XmlTextReader. This class is part of the System.Xml namespace that is included with .Net, and is derived from XmlReader.

XmlTextReader provides sequential, forward-only traversing of the node structure of an XML document. Information about the current node is accessible through attributes such as Name, Type and Value. White space is considered a node, hence the need for line eight to omit it from the output. Here’s a brief sample of the output produced by running Code Block 1 against our sample XML:

Name: name

Type: Element

Value:

Name:

Type: Text

Value: Marc

Name: name

Type: EndElement

Value:

You can see how this method of reading would be useful if you wanted to bring an entire XML document into an array, or find all instances of an element type. But what if you wanted to find specific nodes and move directly to them?

Searching an XML Document

Code Block 2 (see below) demonstrates using XPath to move to specific nodes. The XPath expression on line seven selects all contact nodes in which the contact name is “Marc.” For our purposes, this is only one contact. Line 9 repositions the XPathNavigator so that it currently sits at the contact level. Line 10 moves to the first child node of the contact (the contact’s name). The MoveToNext method is a horizontal node jump – it goes from the first child node of a contact to the second child node, from the second to the third, etc. We loop here to go through all other attributes of the contact. Here is the output:

name: Marc

address: 2 St. Andrews

phone: 555-1234

With a few modifications, this program could become a very useful, generic tool that could accept the XPath expression as a command line argument to execute different searches against the contact list.

In my next column, I’ll take a look at the intricacies of writing an XML document.

Cooney works as a programmer/analyst for a major Canadian book publisher. He can be reached at robert_cooney@hotmail.com.

Exit mobile version