Поделиться через


XML namespace declarations in SyndicationFeed objects

Following on the heels of Steve Maine's post about namespaces in Syndication objects (Feeds, items, etc.), I thought it wise to plug the new stuff in PictureServices.

At the moment, PictureServices implements SLE (Simple List Extensions), and does a pretty crude job of it at that. Time permitting, I will add more support for field and group customization. For now, it serves as an example more than a carrier grade implementation. In any event, I used extension methods in the following type definition to make it easy to insert the right namespace declarations in the feed:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Syndication;
using System.Xml;

namespace Microsoft.DPE.Samples.SyndicationExtensions
{
    // adds custom namespaces to an item or a feed
    public static class SyndicationNamespaceHelper
    {
        public static void DeclareNamespace(this SyndicationFeed feed, 
                                                 String prefix,
                                                 String ns)
        {
            XmlQualifiedName key = new XmlQualifiedName(prefix, NamespaceUris.XMLNamespace);
            feed.AttributeExtensions.Add(key, ns);
        }

        public static void DeclareNamespace(this SyndicationItem item, 
                                                 String prefix,
                                                 String ns)
        {
            XmlQualifiedName key = new XmlQualifiedName(prefix, NamespaceUris.XMLNamespace);
            item.AttributeExtensions.Add(key, ns);
        }
    }
}

The usage model is simple - just new up a feed or an item, then call DeclareNamespace. Piece of cake.

Comments

  • Anonymous
    January 15, 2008
    PingBack from http://msdnrss.thecoderblogs.com/2008/01/16/xml-namespace-declarations-in-syndicationfeed-objects/

  • Anonymous
    January 16, 2008
    This timely, since I've been trying to work out how to add Dublin Core elements to a SyndicationFeed without having the full namespace declaration dumped into every element. The problem that I have is that if the output is RSS for example, there doesn't seem to be a way to get the namespace declaration added to the <rss> element itself - instead it's added to <channel> Unfortunately, won't your example do the same thing? Unless I've missed something the AttributeExtensions collection of the SyndicationFeed maps to attributes on the channel element in an RSS feed, so it looks on the face of it like the same issue I had. I know that's valid XML, but it does differ from what people expect to see. Unless I've got it completely wrong, which happens a lot. Especially since it's 2:16 am and I really should go to bed.