473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
3 7112
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0"...
5
by: NeilL | last post by:
In the XML document I'm trying to create I do the following elem = _doc.CreateElement("Author"); elem.InnerText = "something"; parentElem.AppendChild(elem); Thiw works properly however the...
3
by: Jim Hsu | last post by:
when I use the XmlWebSerivce to response the xmlelement to Web Service client. the ASP.net plumbing work ( the XmlSerializer in WebServices ) will serialize the XML if we can control the wrapper...
3
by: ano | last post by:
Hi, Anyone knows how to get "xmlns" value from XML file? For example, how to check that this xml file has a xmlns or not? Or how to read the xmlns value? <bookstore...
0
by: R. Ian Lee | last post by:
I've built an XSLT file that transforms data to SpreadsheetML format. The XSLT uses a <xsl:call-template/to build each worksheet. For some reason, when I transform the file, it is inserting...
0
by: SolaFide | last post by:
I have an XML doc that looks something like this: <?xml version="1.0" encoding="utf-8"?> <elem1> <elem2 xmlns="http://someurl.com"> <elem3> <item>some stuff here</item> ...
3
by: Lee | last post by:
Hello guys, I am new to XML and working on a XSLT to transforn yahoo shopping search result to html. my problem is the return XML contain xmlns in root element, here is the sample xml: <?xml...
4
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I...
4
markmcgookin
by: markmcgookin | last post by:
Hi, I am creating an MXL doc using XSLT but for some reason it is churning out elements like this <DateTimeLastSaved xmlns="" /> <UserName xmlns="" /> when I delete xmlns="" it works fine,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.