By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,663 Members | 1,477 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,663 IT Pros & Developers. It's quick & easy.

how to add namespace prefix declarations to a new XmlDocument

P: n/a
Hi,

I am creating a new XmlDocument from scratch, but I can't see how I can make
a namespace declaration and associate a prefix with it. For instance in the
following example:

XmlDocument doc = new XmlDocument();
XmlElement el = doc.CreateElement("foo:data");
doc.AppendChild(el);
doc.Save("foo.xml");

the node comes out as just <data> not <foo:data>

now can I associate namespace prefix foo with namespace "http://foo.com/foo"
?

I don't want to add a namespace declaration to every node - I want to have
one appear at the top of the document and be referenced automatically
whenever I add nodes.

Andy
Jan 30 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a


Andy Fish wrote:

I don't want to add a namespace declaration to every node - I want to have
one appear at the top of the document and be referenced automatically
whenever I add nodes.


That is not how the DOM works, the namespace is determined when a node
is being created and not change later depending on where you include,
insert, move, remove it or not.

If you want to create elements in a certain namespace then use the
overloads of CreateElement that allow you to specify namespaceURI and
prefix if needed. Do that for any element you create, later when you
save/serialize the serializer will take care of adding only those
xmlns="someURI" or xmlns:prefix="someURI" namespace declarations that
are necessary e.g.

const string exampleNamespaceURI = @"http://example.com/2006/ns1";
XmlDocument xmlDocument = new XmlDocument();
XmlElement gods = xmlDocument.CreateElement("", "gods",
exampleNamespaceURI);
XmlElement god = xmlDocument.CreateElement("", "god",
exampleNamespaceURI);
god.AppendChild(xmlDocument.CreateTextNode("Kibo") );
gods.AppendChild(god);
xmlDocument.AppendChild(gods);
xmlDocument.Save(Console.Out);

will be serialized/saved as

<gods xmlns="http://example.com/2006/ns1">
<god>Kibo</god>
</gods>

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 30 '06 #2

P: n/a
Thanks Martin, I have got it working now.

From your explanation it would seem that there is no sense in doing
something like this:

doc.CreateElement("g:god")

however, this syntax is explicitly supported. How would it know what
namespace to attach to the element in this case?

Andy
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:e2****************@TK2MSFTNGP12.phx.gbl...


Andy Fish wrote:

I don't want to add a namespace declaration to every node - I want to
have one appear at the top of the document and be referenced
automatically whenever I add nodes.


That is not how the DOM works, the namespace is determined when a node is
being created and not change later depending on where you include, insert,
move, remove it or not.

If you want to create elements in a certain namespace then use the
overloads of CreateElement that allow you to specify namespaceURI and
prefix if needed. Do that for any element you create, later when you
save/serialize the serializer will take care of adding only those
xmlns="someURI" or xmlns:prefix="someURI" namespace declarations that are
necessary e.g.

const string exampleNamespaceURI = @"http://example.com/2006/ns1";
XmlDocument xmlDocument = new XmlDocument();
XmlElement gods = xmlDocument.CreateElement("", "gods",
exampleNamespaceURI);
XmlElement god = xmlDocument.CreateElement("", "god",
exampleNamespaceURI);
god.AppendChild(xmlDocument.CreateTextNode("Kibo") );
gods.AppendChild(god);
xmlDocument.AppendChild(gods);
xmlDocument.Save(Console.Out);

will be serialized/saved as

<gods xmlns="http://example.com/2006/ns1">
<god>Kibo</god>
</gods>

--

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

Jan 30 '06 #3

P: n/a


Andy Fish wrote:
From your explanation it would seem that there is no sense in doing
something like this:

doc.CreateElement("g:god")

however, this syntax is explicitly supported. How would it know what
namespace to attach to the element in this case?


I am not sure why .NET according to its documentation allows a qualified
name there with the single argument method. With the W3C DOM
CreateElement (or createElement) with one string argument stems from DOM
Level 1 which was not namespace aware. W3C DOM Level 2 added support for
namespaces with additional methods createElementNS which you could say
..NET implements by overloading CreateElement to allow various arguments
for namespaceURI or qualified name.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.