13

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

Guy
  • 915
  • 2
  • 14
  • 27
  • Why is the xmlns attribute a problem for you? – John Saunders Dec 02 '10 at 22:08
  • I'm performing a string comparisons. I must avoid it being generated. – Guy Dec 05 '10 at 07:30
  • I tried to reproduce your problem, but the xmlns attribute was not created after I used ImportNode and replaced it. I think the problem is related to how you create the XmlDocument, and how you get newNode. I suggest you add more details and a more complete picture of the code. – Ran Dec 08 '10 at 16:05

6 Answers6

9

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.

MarkXA
  • 4,294
  • 20
  • 22
  • +1: Using `String.Replace` to remove `xmlns=""` feels a little awkward to me, but no more so than using raw string comparison on an XML string. – Brian Dec 06 '10 at 21:44
  • Agreed, it's a bit of a code smell, but trying to do a node-by-node comparison of two XML trees is a PITA, so I can see why comparing the OuterXML could be preferable. – MarkXA Dec 06 '10 at 22:43
  • 1
    Ugly solution, but I guess it works ... I came across the same problem in PowerShell. In my case, doing automation against the Microsoft Azure APIs means that the `xmlns=""` actually breaks validation of the XML document. –  May 01 '14 at 20:12
0

Try:

oldNode.InnerXml = tmpNewNode.InnerXml
iRon
  • 20,463
  • 10
  • 53
  • 79
0

Finally i solved this opening xml file and replace all ocurrences of xmlns "" with a empty string.

Maybe its not elegant solution but its simple and works fine.

//remove void xmlns
File.WriteAllText(filename, Regex.Replace(File.ReadAllText(filename), "xmlns=\"\"", ""));
0

Along the lines of what MarkXA said:

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.

You could manipulate the String for the purpose of the comparison but the nodes in your DOM would not be namespace qualified correctly which could cause you problems later if you tried to use it.

The correct solution would be to build newNode with the proper namespace qualification in the first place. By propery namespace qualification, I mean the namespaces used in the importing DOM.

Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

I got the sames problem when I created an XmlElement like here

XmlElement xmlElement = myXmlDocument.CreateElement("MyElemenent");
myXmlDocument.AppendChild(xmlElement);

after this I got the attribute xmlns="" after saving.

If I use the namespace of the document I could suppress this xmlns attribute.

XmlElement xmlElement = myXmlDocument.CreateElement("MyElemenent",myXmlDocument.DocumentElement.NamespaceURI);
myXmlDocument.AppendChild(xmlElement);

Without the empty xmlns="" my SelectNodes didn't work anymore because the namespace needs to be specified. Solution for this is here (using-xpath-with-default-namespace)

Community
  • 1
  • 1
tivo
  • 337
  • 1
  • 6
-1

add default namespace to your xdoc

Dexion
  • 1,101
  • 8
  • 14