How to make a Google Shopping Feed with C# and serve it through the Web API

I have to confess that every time I have to work with an external API and I'm faced with Xml serialization in C#, my reaction is: no biggie. And then, it hits me. I'll probably have to use custom namespaces. Meh.

The Google Shopping Feed specification uses its fair share of custom namespaces, so let's see how we can deal with that in order to serve the serialized feed through a Web API action method.

As you can see from the link above, we'll be using the Atom 1.0 specification, but using custom namespaces should work as well if you'd rather use the Rss specification.

I recommend you download the Atom 1.0 example file from the specification page before we continue.

Defining the serializable classes

As you can see on the example file, the fun with namespaces starts right off the bat with a custom g namespace that is defined on the root element:
<feed xmlns="" xmlns:g="">

...and then used on every attribute of the entry element.

So, let us define the class for the feed element first:

[XmlRoot("feed"), Serializable]
public class Feed  
    public string Title { get; set; }
    public string Link { get; set; }
    public DateTime Updated { get; set; }
    public List<Entry> Entries { get; set; }

As you can see, nothing too exciting here. A Feed has a couple of simple properties and a collection of Entry objects, each of which represents a product in your shopping feed.

"Hah!" I hear you say. "You forgot to define the g namespace, you big doofus, no wonder you're having trouble with the namespaces!".

Well, no. If you thought you could get away with this:

[XmlRoot("feed", Namespace = "")]
public class Feed  
    // ...

you're out of luck. For one, there's no way (that I know of) to define the g prefix for that namespace, and, to top it up, the feed element requires two namespaces, the other one being the Atom namespace.

Moving on; time for the Entry class:

[XmlType(TypeName = "entry"), Serializable]
public class Entry  
    [XmlElement("id", Namespace = "")]
    public string  Id { get; set; }
    [XmlElement("title", Namespace = "")]
    public string Title { get; set; }
        Namespace = "")]
    public string Description { get; set; }
    [XmlElement("link", Namespace = "")]
    public string Link { get; set; }
    [XmlElement("image_link", Namespace = "")]
    public string ImageLink { get; set; }
    [XmlElement("condition", Namespace = "")]
    public string Condition { get; set; }
        Namespace = "")]
    public string Availablity { get; set; }
    [XmlElement("price", Namespace = "")]
    public string Price { get; set; }
    [XmlElement("brand", Namespace = "")]
    public string Brand { get; set; }
    [XmlElement("mpn", Namespace = "")]
    public string MPN { get; set; }

    [XmlElement("shipping", Namespace = "")]
    public List<Shipping> Shipping { get; set; }

        Namespace = "")]
    public string GoogleProductCategory { get; set; }
        Namespace = "")]
    public string ProductType { get; set; }

and, finally, the Shipping class

[XmlType(TypeName = "shipping"), Serializable]
public class Shipping  
    [XmlElement("country", Namespace = "")]
    public string Country { get; set; }
    [XmlElement("service", Namespace = "")]
    public string Service { get; set; }
    [XmlElement("price", Namespace = "")]
    public string Price { get; set; }

I'm not the kind of person to say "I told you so", but I told you so. Namespace galore.

But this is at least straightforward: the Namespace property on the XmlElement attribute assumes that the given namespace is already defined somewhere so we can just assign the right namespace. But how do we make sure the g prefix is applied on serialization? And that the feed element gets both it's namespaces?

Where the magic happens

We want to end up returning a stream of XML through a Web API action method, and for that we can use the ApiController.Content<T> method since one of the overloads of the method allows us to use a custom formatter.

The Web API has two built in formatters, JSON and XML. Were it not for the custom namespaces we could just tell the Web API to return our Feed class as XML and Robert's your mother's brother, but alas. Time for a custom formatter, then.

In order to create a custom formatter we have to create a class inheriting from XmlMediaTypeFormatter, then, in the constructor of our custom formatter we'll be able to specify any custom XML namespaces we may need. We will also have to overwrite the WriteToStreamAsync method, which is the method that'll be doing the actual serializing of our classes to XML.

So, there you go. Have fun.

What? Oh all right, all right. Here's the code.

Multiple namespaces? No problem!

Let's take a look at the constructor of our custom formatter.

public class NamespacedXmlMediaTypeFormatter : XmlMediaTypeFormatter  
    public XmlSerializerNamespaces Namespaces { get; private set; }
    Dictionary<Type, XmlSerializer> Serializers { get; set; }

    public NamespacedXmlMediaTypeFormatter()
        Namespaces = new XmlSerializerNamespaces();
        Namespaces.Add("g", "");

        Serializers = new Dictionary<Type, XmlSerializer>();

    // We'll take a look at this method in a minute
    public override Task WriteToStreamAsync(...) {}

As you can see, the Namespaces property, of type XmlSerializerNamespaces will hold a collection of XML namespaces. We have to give a prefix to every namespace we add to the collection; in our case this is the g of the Google namespace.

We have them, now where do we use them?

Next up, the method we need to overwrite. According to the documentation, WriteToStreamAsync is:

Called during serialization to write an object of the specified type to the specified writeStream.

So it is not a method we'll have to worry about calling ourselves; the Web API will take care of that once we tell it to use our custom formatter. Here's what it looks like.

public override Task WriteToStreamAsync(Type type, object value,  
    Stream writeStream, HttpContent content, 
    TransportContext transportContext)
    lock (Serializers)
        if (!Serializers.ContainsKey(type))
            // we instantiate the new serializer by passing in 
            // the main XML namespace,
            // in this case the Atom namespace
            var serializer = new XmlSerializer(type, 

            //we add a new serializer for this type
            Serializers.Add(type, serializer);

    return Task.Factory.StartNew(() =>
        XmlSerializer serializer;
        lock (Serializers)
            serializer = Serializers[type];

        var writerSettings = new XmlWriterSettings
            OmitXmlDeclaration = false

        var xmlWriter = XmlWriter.Create(writeStream, writerSettings);
        serializer.Serialize(xmlWriter, value, Namespaces);

One thing to notice is that, as the name implies, it's an asynchronous method and so it has to return a Task. Other that that, it mostly takes care of initializing a Serializer for the type passed in the parameters, and then, already within the asynchronous Task definition it gets the right serializer for the given type and calls the Serialize method on it.

Putting it all together

Now that we have everything nice and ready, how do we use it? Time to create a Web API action method that will return our shiny XML feed ready to be fed to Google.

public IHttpActionResult Get()  
    // here's where you'd fill the properties of the Feed object, 
    // including the Entries collection
    var feed = BuildFeed();

    // and here, finally, we tell the Web API to format the 
    // 'feed' object using our custom formatter.
    return Content(HttpStatusCode.OK, feed, 
        new NamespacedXmlMediaTypeFormatter());

And that's it, really. This action goes in the ApiController that you're gonna use to generate the shopping Feed.

If you have any questions, remarks, or know of a better way to do this, feel free to let me know in the comments!