467,858 Members | 1,315 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,858 developers. It's quick & easy.

Removing xmlns="" from XmlTextWriter output

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
Nov 12 '05 #1
  • viewed: 6822
Share:
3 Replies
* Keith Hill wrote in microsoft.public.dotnet.xml:
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>


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:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Nov 12 '05 #2
That works and thanks for the pointer to FAQ. That's good reading.

--
Keith

"Bjoern Hoehrmann" <bj****@hoehrmann.de> wrote in message
news:41****************@news.bjoern.hoehrmann.de.. .
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

Nov 12 '05 #3

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

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Dickens | last post: by
5 posts views Thread by NeilL | last post: by
3 posts views Thread by ano | last post: by
reply views Thread by R. Ian Lee | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.