views:

148

answers:

1

The subject is probably too short to explain it...

I'm writing out XML files with no namespace stuff at all, for some application. That part I cannot change. But now I'm going to extend those files with my own application-defined element names, and I'd like to put them in a different namespace. For this, the result should look like this:

<doc xmlns:x="urn:my-app-uri">
  <a>existing element name</a>
  <x:addon>my additional element name</x:addon>
</doc>

I've used an XmlNamespaceManager and added my URI with the prefix "x" to it. I've also passed it to each CreateElement for my additional element names. But the nearest I can get is this:

<doc>
  <a>existing element name</a>
  <addon xmlns="urn:my-app-uri">my additional element name</addon>
</doc>

Or maybe also

  <x:addon xmlns:x="urn:my-app-uri">my additional element name</x:addon>

So the point is that my URI is written to every single of my additional elements, and no common prefix is written to the document element where I'd like to have it.

How can I get the above XML result with .NET?

A: 

In general using DOM Level 2 Core methods you would expect:

doc.documentElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:x", "urn:my-app-uri");

to add the namespace declaration to the root element; then if serialised using the namespace algorithm specified in DOM Level 3 LS, that prefix would be used without adding a new xmlns declaration.

Haven't tested that .NET's XmlDocument gets this right, but I would hope so.

bobince