ImportNode creates empty xmlns attribute

后端 未结 6 1800
误落风尘
误落风尘 2021-02-20 08:34

Regrading this code:

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

    if (oldNode.ParentNode != null)
    {
        oldNode.ParentNode.ReplaceChild(tmpNe         


        
6条回答
  •  没有蜡笔的小新
    2021-02-20 08:39

    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)

提交回复
热议问题