Frage

Gibt es einen Unterschied zwischen

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

und

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

Ich bin mit der SyndicationFeed Klasse in .NET, um einen Atom-Feed zu erzeugen, und ich brauche einige Elemente für den Opensearch-Standard hinzuzufügen, aber es hält Hinzufügen von Elementen wie diese oben, wenn ich will es sie wie die hinzufügen frühere.

Der Code:

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

Bearbeiten

Die Wurzel-Feed-Tag sieht wie folgt aus

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

Nach dem Wechsel meinen Code als @Reddog vorgeschlagen, die totalresults Element sieht wie folgt aus

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

Der Code, der den Namensraum zu dem Feed-Tag sieht wie folgt ergänzt

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

Und der Code, der das totalresults Element fügt sieht nun wie folgt

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

Lösung

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/");

Andere Tipps

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>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top