Search code examples
c#xmlxmlserializerapproval-tests

Ignore attributes' order when comparing xml with ApprovalTests


I am trying to Approvals.Verify xml but problem is attributes' order. In few elements they are in different order in my computerand Jenkins. From other question I found to change order of properties in class. That kinda worked: it changed attributes' order but it now Jenkins' order is what it used to be in my computer and vice versa. So no help from changed properties' order in class.

Is there any way to use ApprovalTests to ignore xml attributes' order? Or how can I say exact order for attributes in xml serialization? Or other hints?


Solution

  • According to the XML Standard Section 3.1, the order of attribute specifications in a start-tag or empty-element tag is not significant. So a change in XML attribute ought not trigger a unit test failure.

    And, from the reference source, it appears that the method XmlApprovals.VerifyOrderedXml() normalizes the order of attributes before validation by internally calling XmlUtils.FormatXmlWithOrderedAttributes() which recursively sorts all attributes. Thus this method should meet your needs.