ayomedia's posterous http://ayomedia.posterous.com Most recent posts at ayomedia's posterous posterous.com Wed, 06 Jan 2010 08:33:53 -0800 C# Serialization including Namespaces for Google Products http://ayomedia.posterous.com/c-serialization-including-namespaces-for-goog http://ayomedia.posterous.com/c-serialization-including-namespaces-for-goog

All C# developers know that Serialization can be a pain at the best of times, and it is especially painful if you want to do something out of the "ordinary"!!!

I had a problem when creating a nice little solution for RSS feeds to advertise store products on Google products...

The concept is very simple, Google extended the RSS standard so they could have their own elements (g:condition, g:price..) etc, etc...

I created the class definitions for the RSS classes, RSS, Channel, Item.  Being the anal developer I am, I obviously wanted to keep the property casing correct so added the [XmlElement("")] attributes to them so the serialized version would be to Google's liking and we would both be happy!

For the serialization, I added the Google RSS namespace which was pretty simple using the XmlSerializerNamespaces class... but this is where I realised I'd never had to serialize a class using namespace prefixes before...

I scanned Google and several coding sites for some guidance but I struggled to find anything that would help me out, after a nice cup of English breakfast tea and a chat with The Bizzle, I had another crack at examining the aforementioned [XmlElement] attribute...

This turned up a property called "Namespace", seeing this did bring a little happiness to my afternoon and after extending my existing definitions to include the Google namespace, ran a quick serialization test and to my delight, ta daaaaa! prefixed elements :)

So, the bit you have all been waiting for:

[XmlElement(ElementName = "condition", Namespace = "http://base.google.com/ns/1.0")]

public string Condition { get; set; }

Producing this result:

<g:condition>new</g:condition>

I hope this helps one of you guys, if not then it's something to blog about :)

Ta Ta for now!

Kev

Permalink | Leave a comment  »

]]>
http://posterous.com/images/profile/missing-user-75.png http://posterous.com/users/3sOahYF65bCF ayomedia ayomedia