Datasheet

Chapter 1: AJAX Technologies
16
X M L
As you saw earlier, Listing 1-2 contains a JavaScript function named serialize that serializes a given
credentials object into a string with the following format before this object is sent over the wire to
the server:
usernametbx=username&passwordtbx=password
Listing 1-2 also contains a JavaScript function named deserialize that deserializes an employee object
from a string with the following format:
Shahram|Khosravi|22223333|Some Department|
The serialize and deserialize methods encapsulate the logic that was referred to as the Serializer in
Figure 1-2 .
The great thing about the XML format is that the server- and client-side technologies provide built-in
support for serializing objects into XML and deserializing objects from XML. Listing 1-3 presents a new
version of Listing 1-2 where the
Page_Load server-side method serializes the server data into XML,
which is then sent over the wire to the client, where the
deserialize JavaScript function deserializes an
employee object from the XML.
Listing 1-3: A version of Listing 1-2 that uses XML format
<%@ Page Language=”C#” %>
<%@ Import Namespace=”System.Xml” %>
<%@ Import Namespace=”System.IO” %>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<script runat=”server”>
void Page_Load(object sender, EventArgs e)
{
if (Request.Headers[“MyCustomHeader”] != null)
{
if (Request.Form[“passwordtbx”] == “password” &&
Request.Form[“usernametbx”] == “username”)
{
string xml=””;
using (StringWriter sw = new StringWriter())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
using (XmlWriter xw = XmlWriter.Create(sw, settings))
{
xw.WriteStartDocument();
xw.WriteStartElement(“employeeInfo”);
xw.WriteElementString(“firstName”, “Shahram”);
xw.WriteElementString(“lastName”, “Khosravi”);
xw.WriteElementString(“employeeId”, “22223333”);
xw.WriteElementString(“departmentName”, “Some Department”);
xw.WriteEndElement();
c01.indd 16c01.indd 16 8/20/07 5:40:09 PM8/20/07 5:40:09 PM