Remove XMLNamespace from XML Nodes

  • Thread starter Thread starter daz_oldham
  • Start date Start date
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
 
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");
 
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
 
Back
Top