views:

23

answers:

2

Is there any difference between

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

and

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

I'm using the SyndicationFeed class in .NET to generate an Atom feed, and I need to add some elements for the opensearch standard, but it keeps adding elements like the latter one above when I want it to add them like the former one.

The code:

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

EDIT

The root feed tag looks like this

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

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

<totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/"&gt;1000&lt;/totalResults&gt;

The code that adds the namespace to the feed tag looks like this

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

And the code that adds the totalresults element now looks like this

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

Namespaces

Default namespaces are inherited from the parent element. Or else, you can define new aliases for your children to use with the xmlns:alias= syntax or you can redefine the default namespace to use for an element (and of course it's children) using the xmlns= syntax.

You first example:

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

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

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

Though this means that "myRoot" element is in a different namespace - namely, the default one (with a blank namespace or that defined by it's own parent).

Inserting

In order to actually add the element with the correct namespace, you'll need to use the namespace itself, rather than it's alias ("opensearch").

Therefore, to add your new element you'll need to either grab the namespace from the parent node (or else just know it and have it hard coded).

E.g.

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

But note that you'll have limited or no control over the particular alias given to your namespace. In order to do this, you'll have to take some control over the XML serialization process...

Reddog
I wonder if the namespace in the parent <feed> tag is messed up. See my edit; I'll explain more there.
Jagd
When you say that I have little control over the alias given to the namespace, are you referring to the p1:opensearch attribute that I end up with in the root feed tag?
Jagd
With regard to "control" of the particular alias that got output I believe you'd need to register the namespace/alias higher up in the document when writing/serializing it. I'm not sure how SynchronizationFeed is serialized so afraid I'm not much help from there...
Reddog
Though looking at the result of your Edit, it seems that despite not having the alias prefix that you desire this should achieve the same thing when reading/stylesheeting the output XML.
Reddog
A: 

Nevermind. I realized that I was 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/");
Jagd