473,326 Members | 2,255 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,326 software developers and data experts.

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
5 12392


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 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"...
0
by: johnsocs | last post by:
All I'm trying to write an xml schema for the following xml from the google web service api. In the schema I'm not sure how to describe the soapenv:encodingStyle attribute. Thanks. <?xml...
1
by: Zhenya Sigal via .NET 247 | last post by:
I have the following code: XmlElement parent= m_xmlDoc.CreateElement("parent", "http://tempuri.org/myns"); parent.InnerXml = "<child1>text</child1><child2>text</child2>"; ...
3
by: Keith Hill | last post by:
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...
6
by: TS | last post by:
Hi, i have a problem validating xml against schema. I used http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx validator and it says it is fine. Can you tell me why this doesn't work? ...
2
by: Andy Fish | last post by:
Hi, I have discovered that if I use this syntax: public string bar() { return "hello, world"; } then it is not possible to call the web service using the MS soap toolkit.
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.