XNode.DeepEquals unexpectedly returns false

Using XNode.DeepEquals() to compare xml elements, it unexpectedly returns false in two XML documents, which I think should be equivalent.

Example

 var xmlFromString = XDocument.Parse("<someXml xmlns=\"someNamespace\"/>"); var xmlDirect = new XDocument(new XElement( XNamespace.Get("someNamespace") + "someXml")); Console.WriteLine(xmlFromString.ToString()); Console.WriteLine(xmlDirect.ToString()); Console.WriteLine(XNode.DeepEquals(xmlFromString, xmlDirect)); Console.WriteLine(xmlFromString.ToString() == xmlDirect.ToString()); 

Exit

 <someXml xmlns="someNamespace" /> <someXml xmlns="someNamespace" /> False True 

Strings are considered equal, but XML trees are not. Why?

+7
c # xml linq-to-xml
source share
1 answer

I found out what the difference is, but not why it is different.

In the first form, you have the xmlns attribute. In the second form, you do not do this - not in terms of returning Attributes() . If you explicitly build an XAttribute , DeepEquals will return true :

 var xmlDirect = new XDocument(new XElement( XNamespace.Get("someNamespace") + "someXml", new XAttribute("xmlns", "someNamespace"))); 

As if the namespace is only considered an attribute when converting a tree to a text representation.

+4
source share

All Articles