The following test fails. r1 seems to be missing angle brackets, does anyone know way? I imagine its some sort of encoding error?
var nav1 = XElement.Load(stream).CreateNavigator();
var nav2 = new XPathDocument(stream).CreateNavigator();
using (var r1 = new StringWriter())
using (var r2 = new StringWriter())
{
xslt.Transform(nav1, null, r1);
xslt.Transform(nav2, null, r2);
r1.ToString().Should().Equal(r2.ToString());
}
The problem here is not as far as I can tell semantically equivalent but lexically different xml, but that the resulting xml in the r1 case is missing it’s xml tags. Interestingly, using var nav3 = XElement.Load(stream).CreateReader(); works fine.
Comparing two XML documents using their string representation is not a good idea. While the two documents may be equivalent, they still could have many lexical differences, among them:
I would recommend a better way of doing this — there are many xmldiff tools. You could also build your own XML comparison tool (as I have done for myself) based on my answer to this question.
UPDATE:
After clarifications from the OP:
I investigated and came up with the following conclusion:
The XslCompiledTransform.Transform(IXPathNavigable, XsltArgumentList, Stream) works with either XmlDocument or XPathDocument as the first argument, as is described in the MSDN documentation.
As the Navigator created from an XElement obviously doesn’t fit in this category, the reported problem is observed.
Solution:
Change:
to:
Here is complete code that has been verified to work as expected: