467,903 Members | 1,700 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

xmlns="" in created elements

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 resulting XML file has the following

<Author xmlns="">something</Author>

what I'm trying to understand is why the xmlns="" is added...what I'm trying
to get is just
<Author>something</Author>

I'm sure I've just done something silly or not set some attribute that I
need to use here.
Thanks
--
Neil
Nov 12 '05 #1
  • viewed: 11908
Share:
5 Replies


NeilL wrote:
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 resulting XML file has the following

<Author xmlns="">something</Author>


If you use CreateElement("Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateElement("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateElement("Author", parentElem.NamespaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?
--
Neil
"Martin Honnen" wrote:


NeilL wrote:
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 resulting XML file has the following

<Author xmlns="">something</Author>


If you use CreateElement("Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateElement("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateElement("Author", parentElem.NamespaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


If you regard doc.DocumentElement.NamespaceURI as the "default for the
document" then pass that to CreateElement.
In my understanding there is no such thing as the default for the
document as you can have structures like
<element xmlns="http://example.com/ns1">
<element xmlns="http://example.com/ns2">
<element xmlns="http://example.com/n3" />
</element>
</element>
where the default namespace is declared and redeclared at different levels.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced"-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt
Nov 12 '05 #5
Ok, Thanks, I thnk I get it (close to getting it in any case).

Using
doc.CreateElement("Author", parentElem.NamespaceURI);
fixed the problem that I was having.

Thanks

--
Neil
"Pascal Schmitt" wrote:
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement.NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced"-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mike Dickens | last post: by
1 post views Thread by Zhenya Sigal via .NET 247 | last post: by
3 posts views Thread by Keith Hill | last post: by
reply views Thread by R. Ian Lee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.