Using XNode.DeepEquals()
to compare xml elements, it unexpectedly returns false
on two xml documents that I think should be equivalent.
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());
<someXml xmlns="someNamespace" />
<someXml xmlns="someNamespace" />
False
True
The strings are considered equal, but the XML trees are not. Why?
I've worked out what the difference is, but not why it's different.
In the first form, you have an xmlns
attribute. In the second form, you don't - not in terms of what Attributes()
returns. If you explicitly construct an XAttribute
, DeepEquals
will return true
:
var xmlDirect = new XDocument(new XElement(
XNamespace.Get("someNamespace") + "someXml",
new XAttribute("xmlns", "someNamespace")));
It's as if the namespace only counts as an attribute when converting the tree to a text representation, basically.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With