Stripping namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clive Dixon

    #1

    Stripping namespace

    Given an XmlNode with namespace (NamespaceURI has a value), I want to be
    able to make a clone of it (XmlNode.CloneN ode) but without the namespace.
    How can I strip the namespace from a node?


  • Martin Honnen

    #2
    Re: Stripping namespace

    Clive Dixon wrote:
    Given an XmlNode with namespace (NamespaceURI has a value), I want to be
    able to make a clone of it (XmlNode.CloneN ode) 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.DocumentEle ment;
    XmlNode foo2 = doc.CreateNode( foo1.NodeType, foo1.LocalName, "");
    doc.ReplaceChil d(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/

    Comment

    Working...