More serialization

Amongst the talk about xml serialization, I thought I’d add to the conversation.

I’ve mentioned in previous posts about using xsd.exe to generate c# class files that the XmlSerializer can convert to xml.  I’ve been using these classes in our web application that makes use of the XmlHttpRequest javascript object.  We make the javascript calls to url that return xml and then parse the xml to run the application without having to post the page back.  It works really well in some situations to give the user a real software feel, as opposed to a web page feel.

After learning how to use xsd.exe, along with .xsd files, I wanted to take what I had done one step further. Typically, you would use the c# class files from xsd.exe to serialize your object, and then deserialize the data on the client side, using the same classes. Since I deal in a c# to javascript environment, I couldn’t make use of the deserialization on the client side. What we used to do was write a javascript function that could parse the xml that comes down, and convert it into javascript objects.  It works pretty well, but we end up spending a lot of time writing these parsing functions, as opposed to real code.

So what I ended up doing, was writing my own console application, jsxsd.exe, that does exactly what xsd.exe does, except it writes out the code to deserialize the xml into javascript objects.  This is WAY cool for us, because now when we update our schemas or add new ones, we can immediately get not only the c# class files, but also the javascript objects that go along with them.  The original impetus for the idea was from this blog post, where he wrote an actual javascript implementation of de/serialization, using reflection and the whole bit, and its really, really cool, except that its written in jscript, so it will not run on firefox, which is a huge requirement.

jsxsd.exe is not fully complete yet, I just got it working for the schemas we have, and it doesn’t do any type checking, in the javascript objects, every property is a string, no numbers or dates yet, but it does get all the related xml data into arrays, I’m very pleased with how its working out so far, its great.

BTW: How do you guys post your code in those fieldsets? I’d like to post some code, but I’m not sure how you get it into that format, thanks

4 Comments so far »

  1. Anonymous said,

    Wrote on March 1, 2005 @ 11:19 pm

    I use copysourceashtml, it works a treat. http://www.jtleigh.com/people/colin/software/CopySourceAsHtml/

    The only changes i make to the defaults are to turn off line numbers (makes copy and paste a bitch), tick the ‘embed styles’ box, and if it’s so long that i want it to scroll, i add "overflow: auto; height: 250px;" to the styles for the container div. Oh, and if i feel generous i make it set the tab indent to 2 instead of 4.

  2. Anonymous said,

    Wrote on March 2, 2005 @ 6:49 am

    sweet, thanks Geoff!

  3. Anonymous said,

    Wrote on March 2, 2005 @ 7:38 am

    I use CopySourceAsHtml too, but I really want to know how David Hayden does his fieldsets. Those are fancy!

  4. Anonymous said,

    Wrote on March 6, 2005 @ 3:08 pm

    Hi Ben

    HTTP requests returning XML? That sounds like a web service to me! You should check out the webservice DHTML behavior, as it will manage all the mapping for you on the client end, and .NET’s webservice infrastructure will manage all the Xml serialization on the server side. It is IE only :(

    I wrote a user sample on GotDotNet ages ago that compared this approach with the async callbacks in ASP.NET whidbey (I like the webservice way much better), so if you’d like to see how to use the WebService behavior to call from client side have a look there. http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=fd6acac1-da45-446c-822d-5b2ecdb396a6

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Enter my name (ben) in this box, so I know you're a human.

Comment: