Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Stripping namespace

Question posted by: Clive Dixon (Guest) on June 27th, 2008 07:20 PM
Given an XmlNode with namespace (NamespaceURI has a value), I want to be
able to make a clone of it (XmlNode.CloneNode) but without the namespace.
How can I strip the namespace from a node?


Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
June 27th, 2008
07:20 PM
#2

Re: Stripping namespace
Clive Dixon wrote:
Quote:
Originally Posted by
Given an XmlNode with namespace (NamespaceURI has a value), I want to be
able to make a clone of it (XmlNode.CloneNode) but without the namespace.
How can I strip the namespace from a node?


If the namespace is different then the new node is a not a clone of the
old node.
What you can do is create a new node in a different namespace
respectively in no namespace:

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"<foo xmlns=""http://example.com/ns1""/>");
XmlNode foo1 = doc.DocumentElement;
XmlNode foo2 = doc.CreateNode(foo1.NodeType, foo1.LocalName, "");
doc.ReplaceChild(foo2, foo1);

Note that if the old node has any child elements then you need to apply
the same approach to the child elements as well as those inherit the
namespace.
--

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

 
Not the answer you were looking for? Post your question . . .
182,197 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors