Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / May 2005

Tip: Looking for answers? Try searching our database.

Removing xmlns="" from XmlTextWriter output

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith Hill - 07 Jan 2005 18:11 GMT
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
Bjoern Hoehrmann - 07 Jan 2005 18:29 GMT
* 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
[quoted text clipped - 10 lines]
>    <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
Signature

Björn Höhrmann · mailto:bjoern@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/

Keith Hill - 07 Jan 2005 21:10 GMT
That works and thanks for the pointer to FAQ.  That's good reading.

--
Keith

> If you call CreateElement without any namespace it creates an element
> without any namespace. In XML, this is expressed via xmlns="". If you
[quoted text clipped - 16 lines]
>  * http://www.rpbourret.com/xml/NamespacesFAQ.htm
>  * http://www.jclark.com/xml/xmlns.htm
okie_greg - 23 May 2005 22:18 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.