Naming Collection Items
How do I replace the names of entries in a collection?
By default the names inside of a collection are based on the type or role of the entry.
<CheeseMap>
<KeyValueOfstringCheeseListLjh4bohd>
<Key>Sweden</Key>
<Value>
<string>Adelost</string>
<string>Graddost</string>
<string>Vasterbottenost</string>
</Value>
</KeyValueOfstringCheeseListLjh4bohd>
</CheeseMap>
This provides a certain style of object serialization that is very utilitarian but is not what a format that was crafted by hand has probably picked. People generally like to pick names that describe what an entry is rather than what it does.
You can replicate the particular format names of a collection by using the CollectionDataContract attribute to customize serialization. Ordinarily, collections just work together with serialization through the magic of defaults. CollectionDataContract only shows up in these situations where the defaults are not what you want.
Four changes you might want to make are:
- Changing the list entry name using CollectionDataContract on the list with ItemName=”Cheese”
Changing the key name using CollectionDataContract on the dictionary with KeyName=”Country”
- Changing the value name using CollectionDataContract on the dictionary with ValueName=”Cheeses”
- Changing the dictionary entry name using CollectionDataContract on the dictionary with ItemName=”CheeseRegion”
Putting together those changes, we go from the previous example into this:
<CheeseMap>
<CheeseRegion>
<Country>Sweden</Country>
<Cheeses>
<Cheese>Adelost</Cheese>
<Cheese>Graddost</Cheese>
<Cheese>Vasterbottenost</Cheese>
</Cheeses>
</CheeseRegion>
</CheeseMap>
Comments
- Anonymous
June 12, 2009
Interesting Finds: June 12, 2009