Groups | Blog | Home
all groups > dotnet xml > january 2005 >

dotnet xml : Removing xmlns="" from XmlTextWriter output


Keith Hill
1/7/2005 11:11:48 AM
I am creating an XmlDocument in code and then using XmlTextWriter via
doc.WriteTo(xwriter) to output the result to a text box. I have a root
element that defines a default namespace. However, the sub-elements are
created without any namespaces like so:

XmlElement elem = doc.CreateElement("Foo");
root.AppendChild(elem);

but in the output I get this:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns="http://tempuri.org">
<Foo xmlns="" />
</Root>

How can I get rid of the ' xmlns="" ' attribute on the Foo element?

--
Keith

Keith Hill
1/7/2005 2:10:09 PM
That works and thanks for the pointer to FAQ. That's good reading.

--
Keith

[quoted text, click to view]

Bjoern Hoehrmann
1/7/2005 7:29:16 PM
* Keith Hill wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

If you call CreateElement without any namespace it creates an element
without any namespace. In XML, this is expressed via xmlns="". If you
want to have the Foo element in the same namespace as the Root element
you need to specify the namespace for CreateElement. Remember that

<Root xmlns="http://tempuri.org">
<Foo xmlns="http://tempuri.org" />
</Root>

is equivalent to

<Root xmlns="http://tempuri.org">
<Foo />
</Root>

For more information on XML namespaces try the various MSDN articles
and e.g.

* http://www.rpbourret.com/xml/NamespacesFAQ.htm
* http://www.jclark.com/xml/xmlns.htm
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
okie_greg
5/23/2005 4:18:25 PM

So that it is clear, in order to use a default namespace under the .Net
FX DOM, you have to specify the default? That is odd for a couple
reasons. First, a default can be defined as a value to use in the
absence of a provided value. This behavior of the DOM implementation
is changing that definition to if a particular value is supplied, we
will represent it as the default, yuck!. Second, in parent-child
entity relationships, it is the normal pattern for the child to take on
the attributes of it's parent unless specified as different (just like a
default, only oriented in objects hierarchies). Again, this behavior
changes that relationship, again, yuck!



--
okie_greg
------------------------------------------------------------------------
Posted via http://www.mcse.ms
------------------------------------------------------------------------
View this thread: http://www.mcse.ms/message1334934.html
AddThis Social Bookmark Button