Datasheet

Chapter 1: AJAX Technologies
19
Next, it invokes the WriteStartElement method on the XmlWriter to write a new element named
employeeInfo into the XmlWriter , which in turn writes this element into the StringWriter :
xw.WriteStartElement(“employeeInfo”);
This element will act as the document element of the XML document. Every XML document must have a
single element known as the document element that encapsulates the rest of the XML document.
Page_Load then invokes the WriteElementString method four times to write three elements named
firstName , lastName , employeeId , and departmentName with the specified values into the
XmlWriter , which in turn writes these elements into the StringWriter :
xw.WriteElementString(“firstName”, “Shahram”);
xw.WriteElementString(“lastName”, “Khosravi”);
xw.WriteElementString(“employeeId”, “22223333”);
xw.WriteElementString(“departmentName”, “Some Department”);
Next, Page_Load invokes the ToString method on the StringWriter to return a string that contains
the entire XML document:
xml = sw.ToString();
Then, it sets the Content-Type HTTP response header to the value text/xml to signal the client code
that the server response contains XML data:
Response.ContentType=”text/xml”;
Next, it writes the string that contains the XML data into the server response output stream:
Response.Write(xml);
Finally, it invokes the End method on the Response object to end the response right away and, consequently,
to send the XML document to the client, bypassing the normal rendering routine of the current page:
Response.End();
Now let’s walk through the implementation of the deserialize JavaScript function in Listing 1-3 . This
function invokes the
responseXML property on the XMLHttpRequest object to return the XML
document:
var response = request.responseXML;
var employeeInfo = response.documentElement;
Then, it uses the XML API to extract the employee’s firstname , lastname , employeeid , and
departmentname from the XML document:
c01.indd 19c01.indd 19 8/20/07 5:40:10 PM8/20/07 5:40:10 PM