What is the best way to compare XML files for equality?

前端 未结 7 1287
春和景丽
春和景丽 2020-12-20 12:59

I\'m using .NET 2.0, and a recent code change has invalidated my previous Assert.AreEqual call (which compared two strings of XML). Only one element of the XML is actually

7条回答
  •  温柔的废话
    2020-12-20 13:17

    Because of the contents of an XML file can have different formatting and still be considered the same (from a DOM point of view) when you are testing the equality you need to determine what the measure of that equality is, for example is formatting ignored? does meta-data get ignored etc is positioning important, lots of edge cases.

    Generally you would create a class that defines your equality rules and use it for your comparisons, and if your comparison class implements the IEqualityComparer and/or IEqualityComparer interfaces, then your class can be used in a bunch of inbuilt framework lists as the equality test implementation as well. Plus of course you can have as many as you need to measure equality differently as your requirements require.

    i.e

    IEnumerable.Contains
    IEnumerable.Equals
    The constructior of a Dictionary etc etc
    

提交回复
热议问题