Accessing and Displaying XML Data with .NET
- Data Definition and XPath Intro
- A Simple Example: Three Different Ways
- A Final Example
- Going Forward
Recently Microsoft released Beta 2 of its new platform offering to the general public. Among all the announcements that this platform is more stable and faster, there are many changes to the available classes. With what seems to be a sincere dedication to the XML standard, Microsoft is pushing forward with it in .NET. As always, Microsoft has a knack for integrating products and building fairly useful software that others can digest. This time it's not just ODBC or OLE DB, but also XML classes using similar interfaces. Microsoft is embracing the standard and even stuffing some nice features into its classes. In this article, we review some of what is available to developers who intend to invest in this platform.
Requirements for running the example code in this article are as follows:
.NET Beta 2 SDK.
Windows 2000 and IIS 5.0.
A basic understanding of .NET (namespace compiles, and deployment).
Your favorite text editor. Visual Studio.NET will not be covered.
NOTE
Beta 2 of .NET was recently released with many reported changes in libraries and operation of the platform. If you need a crash course in .NET, then put on your helmet and read my previous article based on Beta 1.
Upon opening the SDK documentation, you will realize that .NET offers a very rich set of XML libraries. The list of available namespaces for XML functionality in .NET is rather large compared to those in other sections:
System.Xml
System.Xml.Xsl
System.Xml.XPath
System.Xml.Schema
System.Xml.Serialization
Beyond the namespaces are many classes to provide much needed or wanted functionality when working with XML data in .NET. As with all programming languages and platforms, you will use some frequently and some not at all. You will find that System.Xml has all the standard W3C DOM classes. System.Xml.Xsl and System.Xml.XPath provide support beyond System.Xml. Table 1 gives a summary of some classes and their roles from differing XML namespaces.
Table 1-Available Namespaces and Classes
System.Xml |
|
XmlDocument |
Implements W3C core DOM levels 1 and 2. It provides access and editing features for XML. |
XmlNode |
A class that represents a particular node element. With this, you can get value and access attributes and other nodes. |
XmlNodeList |
A collection of nodes. It reflects any XML changes through the DOM in real time. |
XmlAttributesCollection |
Represents a collection of XmlAttribute. |
XmlAttribute |
Provides an interface into an Attribute node. |
System.Xml.XPath |
|
XPathDocument |
Used to load the XML, and is designed for XSLT and XPath processing. Therefore, it does not do all the same rule checking as the W3C DOM classes. Use this over System.Xml.XmlDocument primarily for speed, while still using the functionality of XSLT and XPath. |
XPathNavigator |
For read-only random access to data, with an emphasis on XSLT and XPath processing. |
XPathNodeIterator |
Represents a node set. It supports iterating over a set of matches returned from a call to Select on an XPathNavigator instance. |
System.Xml.Xsl |
|
XmlTransform |
Provides a means to load an XSL file and transform a class that inherits from IXPathNavigable. |
This is just a small sampling of what is actually available in .NET, but these are some that the article will demonstrate. With all these classes, it's hard to decide which direction is best for your application. Let's try to lift some of that fog for your own future references and get to building the initial piece of a Web contacts manager.
Data Definition and XPath Intro
No application is complete without data, and we need to define a particular dataset for the rest of this article. We will keep it simple but try to hit on differing aspects of XML:
<personal_information> <contacts> <entry id="0" initials="jhd" keywords="friend, bar" /> <name first="John" middle="Hobo" last="Doe" gender="m" /> <phone type="home" acode="555" number="555-1212" /> <phone type="work" acode="555" number="555-6969" /> <address type="home"> <line seq="1">123 Main Street</line> <line seq="2">#456</line> <city>Manhattan</city> <state>New York</state> <zipcode>10011</zipcode> </address> </entry> </contacts> </personal_information>
That about covers everything, with enough complexity to demonstrate not only element values, but also attributes. This could contain more information, but that is beyond the exercise of this discussion. Copy and paste the XML code and save it to a file named contacts.xml under your working webroot. If you like, more <entry /> nodes can be added for experimentation.
NOTE
Make sure that any XML code added is well formed; otherwise, it could cause problems. XML syntax can be quickly verified by opening the saved file in Internet Explorer. .NET does produce errors with decent debugging information, and I suggest trying out the tracing and other debugging functionality.
For those who are not familiar with it, XPath is a great mechanism for selecting nodes with complex or simple conditions. If you need just a set of nodes with an attribute that meets a certain condition and is second in the list, then XPath is the solution. For example, this next line gives you the second entry in the list of selected nodes:
/personal_information/contacts/entry[position() = 2]
Far more can be done with XPath, but it could take up books on its own. You can read more about XPath at Zvon. Now that you're familiar with the data and have seen XPath, let's examine some of the .NET XML offerings.