Building an XmlSerializer in Javascript

Anders Norås has an interesting blog entry entitled A JavaScript XmlSerializer where he shows how to build a class equivalent to the .NET Framework’s System.Xml.Serialization.XmlSerializer class in Javascript. He writes

In ASP.NET 2.0 it is possible to invoke server events from client side script without posting back the page. This is supported through a new mechanism called script callbacks. For more information on this technology, read Dino Espositio’s excellent “Cutting Edge” article on the subject.

Mostly out-of-band calls have been used with fairly simple return values such as strings and numerals. The “advanced” uses have typically been passing arrays as comma separated lists. This has greatly limited the applicability of the technology and created a wide functionality gap between the object oriented programming environment of the server world and the more primitive environment in the browser…

A JavaScript XmlSerializer

One of the most celebrated classes in the .NET framework is the XmlSerializer class. This class enables you to serialize objects into XML documents and deserialize XML documents into objects. As we all know, XML documents are represented as strings, so it is simple to pass an XML document as either a parameter or a return value on an out-of-band call.

By implementing a client side XML based serialization and deserialization it would be possible to pass an object from a client script to a server method and vice versa. There are of course huge differences between the powerful .NET platform and the simple JavaScript language, but these have little impact on a client to server communications channel as it would only make sense to pass data transfer objects.

Definitely an interesting bit of code. What is also very interesting is that he has a previous article entitled Declarative JavaScript programming where he implements metadata annotations (akin to .NET Framework attributes) for Javascript. Excellent stuff.

Comments (3)

  1. Ken says:

    This looks cool! It seems to work like the I’ve been using that control to pass back arrays of classes. Then in my javascript I can refer to the property names and iterate through the array instead of having to use an array of plain old strings and index numbers. Ken.

  2. Mark Wilson says:

    I thought you and your visitors might like to read an online XmlSerializer tutorial at TopXML



  3. yeah that was really interesting to read about. maybe I will use it in my future work