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

how to add namespace prefix declarations to a new XmlDocument

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
3 27016


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


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

Similar topics

1
by: Romeo Disca | last post by:
Hello newsgroup, i'm new to xml - what's wrong with this piece code here? i have these two files: test.xml ---- <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE a SYSTEM "test.ent"
2
by: Frank Wilson | last post by:
Is there a way to change the namespace prefix of a document? I am sending a document to an application that requires that every element be prefixed with "sm". My inbound document has every...
2
by: Dale Anderson | last post by:
I have a schema that I'm trying to read. The schema has an element named 'GrantApplication' and one with a namespace prefix named 'SF424:GrantApplication'. When I try to read this schema in, I...
1
by: John Lee | last post by:
Hi, if I have xml document <root xmlns:a="myNS1" xmlns:b="myNS2" xmlns="myNS"> <a:node1>value1</a:node1> <b:node2>value2</b:node2> <node3>value3</node3> </root> if I load the above xml in...
4
by: jb | last post by:
I have discovered that when the WSDL is auto-generated in .NET (i.e. http://.../MyService.asmx?WSDL): * Prior to SP1, it generated xmlns:s0="http://mynamespace/" in <wsdl:definitions>, and then...
2
by: james.cssa | last post by:
I want to declare namespace prefix in the Envelope element (i.e. xmlns:xsi and xmlns:xsd) so that the document will be serialized to look like the following. However, I don't know the right way to...
1
by: Tanja Schaettler | last post by:
Hello! I have an existing SOAP message which looks like: <?xml version="1.0" encoding="UTF-8" ?> <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"...
2
by: scottpet | last post by:
Hi, I want to add a namespace prefix to the root node of an object I am serializing to XML. I have been reading though this article:...
3
by: Markus Bauer | last post by:
Hello, I have a problem with the namespace prefix. I need to add nodes to a xml file that is not created by me. It has a namespace with the prefix "NFD". So an already existing node looks...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.