English 中文(简体)
Confused about namespaces in Atom feed
原标题:

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">

After changing my code as @Reddog suggested, 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 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");
最佳回答

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/");
问题回答

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/">
    <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...

To be more complete.

Set specify namespace on channel element with:

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

And specify namespace on totalResults with:

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

That will give you:

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




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签