Confused about namespaces in the Atom feed

Is there any difference between

<opensearch:totalResults>1000</opensearch:totalResults> 

and

 <totalResults xmlns="opensearch">1000</totalResults> 

I use the SyndicationFeed class in .NET to create an Atom feed, and I need to add some elements for the opensearch standard, but it continues to add elements like the previous one when I want it to add them as the former.

The code:

 feed.ElementExtensions.Add("totalResults", "opensearch", "2"); 

EDIT

The root feed tag is as follows:

 <feed xml:lang="en-US" p1:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:p1="xmlns" xmlns="http://www.w3.org/2005/Atom"> 

After changing my code as suggested by @Reddog, the totalresults element looks like this:

 <totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">1000</totalResults> 

The code that adds the namespace to the feed tag is as follows:

 feed.AttributeExtensions.Add( new XmlQualifiedName("opensearch", "xmlns"), @"http://a9.com/-/spec/opensearch/1.1/"); 

And the code adding the totalresults element now looks like this:

 feed.ElementExtensions.Add("totalResults", @"http://a9.com/-/spec/opensearch/1.1/", "1000"); 
+4
source share
3 answers

Nevermind I realized that I am adding the namespace incorrectly. It should be

 feed.AttributeExtensions.Add( new XmlQualifiedName("opensearch", "http://www.w3.org/2000/xmlns/"), "http://a9.com/-/spec/opensearch/1.1/"); 
+3
source

Namespaces

By default, namespaces are inherited from the parent. Or you can define new aliases for your children to use with the xmlns:alias= syntax or you can override the default namespace that will be used for the element (and, of course, its children) using the xmlns= syntax.

First example:

 <opensearch:totalResults>1000</opensearch:totalResults> 

It is required that the namespace alias "opensearch" be defined by the parent element - possibly in a different namespace. For example:

 <myRoot xmlms:opensearch="http://a9.com/-/spec/opensearch/1.1/"> <opensearch:totalResults>1000</opensearch:totalResults> </myRoot> 

Although this means that the "myRoot" element is in a different namespace, namely, by default (with an empty namespace or with its own parent).

Insert

To really add an element with the correct namespace, you will need to use the namespace and not its alias ("opensearch").

Therefore, to add a new element, you need to either grab the namespace from the parent node (or just know it and hard code).

eg.

 feed.ElementExtensions.Add("totalResults", "http://a9.com/-/spec/opensearch/1.1/", 1000); 

But note that you are limited or will not control the specific alias specified for your namespace. To do this, you will need to control the XML serialization process a bit ...

+2
source

To be more complete.

Define the namespace in the channel element with:

 feed.AttributeExtensions.Add( new XmlQualifiedName("opensearch", XNamespace.Xmlns.ToString()), "http://a9.com/-/spec/opensearch/1.1/"); 

And specify the namespace for totalResults with:

 feed.ElementExtensions.Add("totalResults", "http://a9.com/-/spec/opensearch/1.1/", "1000"); 

This will give you:

 <opensearch:totalResults>1000</opensearch:totalResults> 
0
source

All Articles