10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

Create successful ePaper yourself

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

The XMLHttpRequest Object<br />

As you can see, the data contained within the XML file is rendered in the page in exactly the same way it<br />

is stored within the file.<br />

As mentioned previously, this might be okay for simple scenarios that require only a single unit of data<br />

or data that is in a proprietary format, but for more complex scenarios you’ll want to use the<br />

responseXML property.<br />

Using the responseXML Property<br />

In a majority of scenarios, you will want to return multiple result items. This might be a list of names to<br />

display in a drop-down list, a list of customers, or an object representation. The XML format is ideally<br />

suited to this, and direct support for this format is provided by the responseXML property of the<br />

XMLHttpRequest object. This property is a standard XML document object that can be examined and<br />

parsed using W3C DOM node tree methods and properties.<br />

Try It Out<br />

For detailed reference material on the XML Document Object Model, visit http://msdn<br />

.microsoft.com/library/en-us/xmlsdk30/htm/xmmscxmlreference.asp.<br />

Returning Response Data As an XML Document Object<br />

You can continue to modify the code example to extract values from the XML data that was retrieved<br />

and displayed in the previous example. Rather than reproduce the entire previous code sample, we will<br />

show only the modified section that replaces the added code from the previous example that used the<br />

responseText property. Examine the following code:<br />

if ( xmlHttpObj.readyState == READYSTATE_COMPLETE )<br />

{<br />

var doc = xmlHttpObj.responseXML;<br />

var node = doc.selectSingleNode(“//Customers/Customer/Firstname/text()”);<br />

document.getElementById(“divResults”).childNodes[0].nodeValue = node.nodeValue;<br />

}<br />

Instead of simply displaying the entire set of returned data, you are extracting the text value of the first<br />

instance of the node. You do this by utilizing the selectSingleNode method, which<br />

takes an X Path expression as an argument and returns an XML node object if one is found or NULL if the<br />

X Path query is unsuccessful. <strong>With</strong> the returned node object, you assign the nodeValue property of the<br />

first element of the childNodes property, which itself is a property of the divResults element to the<br />

text value of the node.<br />

For those unfamiliar with the X Path language, you can think of it as a dynamic query language for<br />

XML documents that operates in a similar way to ANSI SQL for databases. Nodes, elements, and data<br />

can be queried and returned using X Path expressions. X Path expressions can contain conditional<br />

expressions and be quite complex. For a detailed reference on X Path expressions and the associated<br />

syntax, visit the W3C site at www.w3.org/TR/xpath, or for something a little more readable, try<br />

www.topxml.com/xsl/XPathRef.asp.<br />

This example is still fairly simplistic, though. Now, you’re going to take your newfound knowledge and<br />

apply this by creating a simple web page that allows you to make a selection from a list of customers<br />

and display the fullname and email address by doing a server-side lookup into your XML data file<br />

asynchronously.<br />

87

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

Saved successfully!

Ooh no, something went wrong!