2

I am developing a WCF method that returns object data, including a Dictionary property. The XML contains a large amount of markup and I was wondering if there was means of reducing the markup.

Below is an example of the current XML output for the Dictionary:

<gases>
 <a:KeyValueOfstringfloat>
  <a:Key>CH4</a:Key> 
  <a:Value>40</a:Value> 
 </a:KeyValueOfstringfloat>
 <a:KeyValueOfstringfloat>
  <a:Key>CO2</a:Key> 
  <a:Value>60</a:Value> 
 </a:KeyValueOfstringfloat>
</gases>

For each additional gas element added to the collection, an extra <a:KeyValueOfstringfloat> along with its content is added. Is it possible to customise the formatting to something along the lines of:

<gases>
 <key>value</key>
</gases>

Thanks in advance.

Peppermintology
  • 9,343
  • 3
  • 27
  • 51

2 Answers2

0

There are a couple of options but both involve not using the .NET Dictionary type for your collection. Given your desired output, look at the accepted answer to this question.

Otherwise, if you want simple XML, you need to use a simple class structure. Define a gases class that contains a property named key that is an array of string. I'm pretty sure the resulting soap XML probably looks like what you displayed.

In both of these cases, that may not be what you really want. I'm guessing that the key element in the XML needs to be different for each key/value pair but you should be able to design your classes to handle this (create an item class with name and value string properties for example). If you want to work with the gases class as a dictionary, you'll have write code to do it.

UPDATE: Here is an example of how configurable the CollectionDataContract attribute can be:

[CollectionDataContract(
    Name = "Cities",
    ItemName = "city",
    KeyName = "cityName",
    ValueName = "population")]
public class CityList
    : IDictionary<string, int>,
      IEnumerable<System.Collections.Generic.KeyValuePair<string, int>>
{
/// snipped
}
Community
  • 1
  • 1
Sixto Saez
  • 12,610
  • 5
  • 43
  • 51
  • Does the collectiondatacontract allow for none collection based members to be included in the same datacontract, or is a seperate datacontract required, then both are implemented in a messagecontract? – Peppermintology Apr 19 '11 at 13:36
  • Not sure what it is you're asking but the link below provides you with everything you need to know about using the CollectionDataContract attribute. Link: http://msdn.microsoft.com/en-us/library/aa347850.aspx – Sixto Saez Apr 19 '11 at 14:46
0

Maybe not exactly what you're looking for, but GZip-ing the messages over the wire will help a lot

TheNextman
  • 12,428
  • 2
  • 36
  • 75
  • The idea of compressing the data over the wire has been discussed, however, we'd prefer to explore other options as resources on the device being interacted with are limited. It may be though that compression is the more suitable option. All ideas are welcome though so thank you for yout input. [: – Peppermintology Apr 19 '11 at 13:35