<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Emmer Inc &#187; XML</title>
	<atom:link href="http://blog.emmerinc.be/index.php/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.emmerinc.be</link>
	<description>Tales from a .NET developer who's making the jump to the iPhone &#38; App Store wonderland.</description>
	<lastBuildDate>Mon, 06 Sep 2010 19:43:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Reduce the XML WCF response size</title>
		<link>http://blog.emmerinc.be/index.php/2009/03/23/reduce-the-xml-wcf-response-size/</link>
		<comments>http://blog.emmerinc.be/index.php/2009/03/23/reduce-the-xml-wcf-response-size/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 11:40:48 +0000</pubDate>
		<dc:creator>Yannick Compernol</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[serialization]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://blog.emmerinc.be/?p=288</guid>
		<description><![CDATA[I recently received a request for a post on how to reduce the XML response size of a WCF service easily. When developing for mobile devices (like the iPhone), it&#8217;s crucial to keep loading times as small as possible. Reducing the size of the XML response can be done in two ways: either the actual [...]]]></description>
			<content:encoded><![CDATA[<p>I recently received a request for a post on how to reduce the XML response size of a WCF service easily. When developing for mobile devices (like the iPhone), it&#8217;s crucial to keep loading times as small as possible. Reducing the size of the XML response can be done in two ways: either the actual data or the encapsulating XML elements is/are treated. Changing the actual data probably has a severe impact on you&#8217;re applications inner workings, that&#8217;s why we&#8217;ll focus on the latter. Here&#8217;s how:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>DataContract<span style="color: #000000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;p&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Product
<span style="color: #000000;">&#123;</span>
   <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;n&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> Name <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #000000;">&#91;</span>DataMember<span style="color: #000000;">&#40;</span>Name <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;a&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
   <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Amount <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>By adding the name attribute we can specify how the XML element will be called, by default it uses the name of the class or property. The original response is 64 characters long, while the tweaked is only 35 characters, a reduction in size of more than 40%. This also works when the response is or contains a list or products.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">// Original response
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MacBook<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Amount<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>123<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Amount<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Product<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
// Response after tweaking
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MacBook<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/n<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>123<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>In the end it&#8217;s a trade-off between overall readability and performance, if you&#8217;re consuming the service in .NET with WCF, then you won&#8217;t even know that the response has been tampered with, since it&#8217;s all done in the background. If you&#8217;re calling the WCF service from for instance Objective-C and are parsing the result, you&#8217;ll have to adjust the parsing to use the new XML element names.</p>
<p>In the case of an application for a mobile device, the benefits (faster loading) outweigh the disadvantages. You even get an added bonus: less bandwidth consumption on the server.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.emmerinc.be/index.php/2009/03/23/reduce-the-xml-wcf-response-size/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
