Remove XMLNamespace from XML Nodes

D

daz_oldham

Hi Everyone

I have an xml document, and for the purposes of my application, I am
going to take a node of the xml (child), add a child to it and then
store it into the database for later use.

So I start with:

<doc xmlns:"http://www.xml.com">
<stuff>
<... />
</stuff>
<child>
<element value="x" />
</child>
</doc>

And want to store:

<child>
<newelement value="w" />
<element value="x" />
</child>

However, because my XmlDocument() is under the given namespace, when I
have created the above node, I am getting the namespace attributed to
it like so:

<child xmlns="http://www.xml.com">
<newelement value="w" xmlns="" />
<element value="x" />
</child>

Is there any way I can stop this from happenening, because later on in
the process, the XmlNode will be appended to an XmlDocument under this
namespace.

Many thanks

Darren
 
M

Martin Honnen

daz_oldham said:
However, because my XmlDocument() is under the given namespace, when I
have created the above node, I am getting the namespace attributed to
it like so:

<child xmlns="http://www.xml.com">
<newelement value="w" xmlns="" />
<element value="x" />
</child>

Is there any way I can stop this from happenening, because later on in
the process, the XmlNode will be appended to an XmlDocument under this
namespace.

An element or attribute node gets its namespace at creation time and it
does not change later when you insert the node somewhere. Thus if you
want to create an element in the namespace http://example.com/2006/xml
then you need to use a namespace aware creation method e.g.
XmlElement newElement = xmlDocument.CreateElement("newelement",
"http://example.com/2006/xml");
 
D

daz_oldham

Will this have an effect later on when I use the child, because for the
purposes of what I am doing, I ideall don't want to keep the namespace
information.

If it is that re-specifying the namespace more than once won't cause a
problem, then I should be okay... however if it is, then I am going to
have a problem with my validation.

(if it helps, i'm a bit of a novice at xmldom etc)

I will in effect end up with something like this:

<root xmlns="http://x.com" >
<otherdata>
<nodes>...</nodes>
<child>
<savednode xmlns="http://x.com">
<childnode xmlns="" />
<savednode>
<savednode xmlns="http://x.com">
<childnode xmlns="" />
<savednode>
<savednode xmlns="http://x.com">
<childnode xmlns="" />
<savednode>
</child>
</root>

Many thanks for your help Martin - what do you think?

Darren
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top