KevinUp

real world programming

xml bloat got you bloated?

Posted by kevinup on September 15, 2007

I’ve been spending a lot of time recently on performance. We finally got some network resources assigned to our group, and they discovered something I should have notice a LONG time ago.

Our architecture uses a web server to application server model. Its works out great for separating business logic and security, there are other reasons to use this architecture, but thats not important for my point.

We use web services(Soap/XML) to talk between the web server and app server. We were having typical requesting in the size range of 300-2000k. It is ridiculous that some of our request were that large. Here’s why:

Lets say I have a sales person, with a first/last name, sales person type with some other data, it will look something like this in XML:

<salesperson><id>2000</id><createdon>06/07/2007</createdon><firstname>kevin</firstname><lastname>upchurch</lastname><salespersontype><id>1</id><code>active</code><description>active sales person</description></salespersontype></salesperson>

The above xml is ~240 bytes. With a couple cool attributes you can reduce this the extra bloat for nothing. The first is

XmlType

, and the other is

XmlElement

On your class your use the type like this:

[XmlType("s")]
public class Salesperson

and on your properties you use the element like this:

[XmlElement("f")]
public string FirstName

So now, with your attributes in place you can reduce your XML to this:

<s><i>2000</i><co>06/07/2007</co><f>kevin</f><l>upchurch</l><st><i>1</i><c>active</c><d>active sales person</d></st></s>

In other words this is ~120 bytes. Now for the disclaimer, your WSDL’s are going to be super messed up. I would only recommend doing this if your are using web services within an internal application. I found using Dave Donaldson’s web service proxy generator helps, just make sure you check the ‘remove entity types’. Also, I wouldn’t use this on all your classes, and properties, just a couple well used ones, there isn’t any reason to get too crazy. Any finally, remember that only properties that have getters and setters get serialized to XML.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

 
%d bloggers like this: