472,146 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

programatically remove namespace from XmlDocument

Hi,
I'm writing a program that loads an xml document, modifies it, validates
it with an xsd schema, then send it on to another application for
processing. I have no control over this aplication.

The problem is this: for some reason, this application rejects any xml
documents with a namespace (as crazy as that sounds). So I need to strip
the xmlns="www.MyNamespace.com" attribute from the document before
passing it on.

This seems like it should be an easy thing to do... but I can't figure
out how to do it. I've tried

MyXml.DocumentElement.Attributes.RemoveAll();

and even

MyXml.LoadXml("<RootNode>" + OrderXml.DocumentElement.InnerXml +
"</RootNode>");

but neither of those work. RemoveAll() has no effect on the xmlns
attribute, and re-loading the xml with a new root node only moves the
xmlns attribute down to the next child of RootNode.

Any ideas?

thanks,
Gabe
Nov 12 '05 #1
2 15624
If only the attribute that you want to get rid off, maybe using regular
expression is quick and dirty way to do it. Even if you need to remove the
prefix you still can use Regex. This regex should help you match the
attributes:

<.*?(xmlns="www.MyNamespace.com").*?>

--
Victor Hadianto
http://synop.com/Products/SauceReader/

"Gabe Moothart" <ga**@imaginesystems.net> wrote in message
news:Os**************@tk2msftngp13.phx.gbl...
Hi,
I'm writing a program that loads an xml document, modifies it, validates
it with an xsd schema, then send it on to another application for
processing. I have no control over this aplication.

The problem is this: for some reason, this application rejects any xml
documents with a namespace (as crazy as that sounds). So I need to strip
the xmlns="www.MyNamespace.com" attribute from the document before passing
it on.

This seems like it should be an easy thing to do... but I can't figure out
how to do it. I've tried

MyXml.DocumentElement.Attributes.RemoveAll();

and even

MyXml.LoadXml("<RootNode>" + OrderXml.DocumentElement.InnerXml +
"</RootNode>");

but neither of those work. RemoveAll() has no effect on the xmlns
attribute, and re-loading the xml with a new root node only moves the
xmlns attribute down to the next child of RootNode.

Any ideas?

thanks,
Gabe

Nov 12 '05 #2
Gabe Moothart wrote:
The problem is this: for some reason, this application rejects any xml
documents with a namespace (as crazy as that sounds). So I need to strip
the xmlns="www.MyNamespace.com" attribute from the document before
passing it on.


You can't remove namespace for XmlDocument in-memory. Namespace is
integral part of name, so you have to recreate any element, which is in
the namespace.
Alternatively strip namespaces while writing XmlDocument out using some
custom XmlWriter, e.g. take a look at
http://blogs.msdn.com/kaevans/archiv...02/206432.aspx

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JT | last post: by
reply views Thread by XML newbie: Urgent pls help! | last post: by
4 posts views Thread by XML newbie: Urgent pls help! | last post: by
2 posts views Thread by daz_oldham | last post: by
5 posts views Thread by Tim Mackey | last post: by
reply views Thread by Saiars | last post: by

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.