- Data Definition and XPath Intro
- A Simple Example: Three Different Ways
- A Final Example
- Going Forward
A Simple Example: Three Different Ways
With some of the namespaces listed previously, which you can also find in the .NET SDK documentation, you can easily build a small Web page that utilizes the XML functionality of .NET. For simplicity, the code will list the full name of every contact in the contacts.xml file using the differing methods surrounding System.Xml, System.Xml.Xsl, and System.Xml.XPath.
With System.Xml, you can build a node set with XPath out of XmlDocument by using selectNodes or selectSingleNode (see Listing 1).
Listing 1: W3C DOM Traversal
<%@Page language="C#" %> <%@Import Namespace="System.Xml" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Create XmlDocument and load the XML from file XmlDocument myXmlDocument = new XmlDocument(); myXmlDocument.Load(new XmlTextReader(Server.MapPath[ccc] ("/contacts.xml"))); XmlNode oNode = myXmlDocument.DocumentElement; XmlNodeList oList = oNode.SelectNodes("./contacts/entry/[ccc] name"); foreach (XmlNode oEntry in oList) { XmlAttributeCollection oAttributes = oEntry.Attributes; Response.Write(oAttributes["first"].Value + " " + [ccc] oAttributes["middle"].Value + oAttributes["last"].Value + "<br>"); } } </script>
Along with the W3C standard, you trade off slower iteration for all the rule-checking and node-identity functionality proposed in the XML standard. It's not a bad thing, but in programming, it might not be exactly the set of features needed to complete the job.
Another way you could achieve similar results with .NET xml capabilities is through an XSL transform (see Listing 2).
Listing 2: XSL Transform
---------------XSL --------------------------- <xsl:stylesheet xmlns:xsl = "http://www.w3.org/1999/XSL/[ccc] Transform" version = "1.0"> <xsl:template match="/"> <xsl:for-each select="//contacts/entry/name"> <xsl:value-of select="@first" /><xsl:text> </xsl:text>[ccc] <xsl:value-of select="@middle" /><xsl:text> </xsl:text><xsl:[ccc] value-of select="@last" /><br /> </xsl:for-each> </xsl:template> </xsl:stylesheet> ----------------------------------------------- <%@Page language="C#" %> <%@Import Namespace="System.Xml" %> <%@Import Namespace="System.Xml.Xsl" %> <%@Import Namespace="System.Xml.XPath" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { XPathDocument oDoc = new XPathDocument(Server.MapPath[ccc] ("/contacts.xml")); XslTransform oTrans = new XslTransform(); oTrans.Load(Server.MapPath("/contacts_html.xsl")); StringWriter oStream = new StringWriter(); oTrans.Transform(oDoc, null, oStream); Response.Write(oStream.ToString()); } </script>
The real downside here is that two different code sets are being maintained instead of just one. Granted, the HTML is abstracted into an XSL file that can be easily replaced, but you also lose advantages of .NET, such as HtmlControls, that obey HTML 3.2 standards.
The System.Xml.XPath namespace provides classes specifically for XPath and XSLT processing. System.Xml and System.Xml.Xsl classes also use the System.Xml.XPath classes to support features. Obviously, the selection is accomplished with XPath syntax, but there is also a mechanism for doing a sort (covered in the example). Let's take a quick look at what it takes to access and display the contact information defined in the XML (see Listing 3).
Listing 3: XPath Namespace
<%@Page language="C#" %> <%@Import Namespace="System.Xml.XPath" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { XPathDocument oDoc = new XPathDocument(Server.[ccc] MapPath("/contacts.xml")); XPathNavigator oNav = oDoc.CreateNavigator(); oNav.MoveToRoot(); XPathNodeIterator oIterator = oNav.Select[ccc] ("personal_information/contacts/entry/name"); while (oIterator.MoveNext()) { oIterator.Current.MoveToAttribute("first", ""); Response.Write(oIterator.Current.Value); oIterator.Current.MoveToParent(); oIterator.Current.MoveToAttribute("middle", ""); Response.Write(" " + oIterator.Current.Value + " "); oIterator.Current.MoveToParent(); oIterator.Current.MoveToAttribute("last", ""); Response.Write(oIterator.Current.Value + "<br>"); oIterator.Current.MoveToParent(); } } </script>
Of note is that XPathDocument is read-only, and absolutely no editing can happen with this class. On the upside, this is part of what makes it fast for XPath processing. It seems like a bit more work is done getting attribute values with the XPathNodeIterator, but it's hard to tell underneath. Unfortunately, performance analysis is beyond the scope of this article. Although you could run some numbers on the three code samples, I would not put much faith in them initially. For now, we have to assume that Microsoft is telling the truth in the SDK documentation about the System.Xml.XPath classes.
As you can see, choosing the right set of classes for the job is the name of the game. With that said, let's roll up our sleeves so that we can put some of these classes together to build a namespace for initial development of the Web contacts manager.