Search code examples
c#.netxmlxmldocument

ImportNode creates empty xmlns attribute


Regrading this code:

   var tmpNewNode = xdoc.ImportNode(newNode, true);

    if (oldNode.ParentNode != null)
    {
        oldNode.ParentNode.ReplaceChild(tmpNewNode, oldNode);
        return true;
    }

tmpNewNode is created with empty xmlns attribute (xmlns=""). Any suggestion how can I avoid it?

10x


Solution

  • What's probably happening here is that newNode comes from a document with no namespace declared, but oldNode is in a document with a namespace. In this situation, the node takes its blank namespace over to the new document and it shows up explicitly. To be honest, if it's only a problem for a string comparison, it won't hurt to just remove all instances of xmlns="" from the XML string before you work with it.