I am using the .NET SyndicationFeed class and have added some of my own extensions using SyndicationItem.ElementExtensions.Add() as well as setting SyndicationItem.Content to some Xml content.
My problem is that my namespace shows up multiple times in the XML output. Ideally I would apply a xmlns attribute to the root node and use its alias throughout the document.
I have seen examples that discuss using SyndicationFeed.AttributeExtensions as seen here. For example:
feed.AttributeExtensions.Add(
new System.Xml.XmlQualifiedName("myns", "http://www.w3.org/2000/xmlns"),
"http://myNamespace.com");
But, none of these examples show how to utilize the namespace later. For example, here are two ways I extend the feed:
XNamespace myNs = "http://myNamespace.com";
SyndicationItem item = new SyndicationItem();
XElement myMetadata = new XElement(myNs + "metadata");
myMetadata.Add(new XElement(myNs + "meta1", "value1"));
myMetadata.Add(new XElement(myNs + "meta2", "value2"));
item.Content = SyndicationContent.CreateXmlContent(myMetadata);
XElement myExtensions = new XElement(myNs + "myExtensions");
myExtensions.Add(new XElement(myNs + "ext1", "value1"));
myExtensions.Add(new XElement(myNs + "ext2", "value2"));
item.ElementExtensions.Add(myExtensions);
Hopefully I'm missing something simple. With the AttribuetExtensions.Add() method further above, my feed has the following for the initial XML:
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:a10="http://www.w3.org/2005/Atom" version="2.0">
<channel p3:myns="http://myNamespace.com" xmlns:p3="http://www.w3.org/2000/xmlns">
Granted, I'd prefer that the xmlns for myns be on the root rss node and not the channel, but I can live with it being on the channel. Unfortunately, the syndication item xml looks like:
<item>
...
<a10:content type="text/xml">
<metadata xmlns="http://myNamespace.com">
<meta1>value1</meta2>
<meta2>value2</meta2>
</metadata>
</a10:content>
<myExtensions xmlns="http://myNamespace.com">
<ext1>value1</ext1>
<ext2>value2</ext2>
</myExtensions>
</item>
Of course, what I'd prefer to see is:
<item>
...
<a10:content type="text/xml">
<myns:metadata>
<meta1>value1</meta2>
<meta2>value2</meta2>
</myns:metadata>
</a10:content>
<myns:myExtensions>
<ext1>value1</ext1>
<ext2>value2</ext2>
</myns:myExtensions>
</item>
Is there some special way of linking the namespace defined by SyndicationFeed.AttributeExtensions with that used when extending a SyndicationItem?
You may want to decalre the namespace for the feed like so:
Then you can declare your XNamespace and use it in your element creation.
I have created one here for an rdf element but you can do it for any of your custom types. You could even add the creation of your namespaces into an extension method of the SyndicationFeed and SyndicationItem: