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

前端 未结 7 1254
春和景丽
春和景丽 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:33

    I made a method to create simple XML paths.

    static XElement MakeFromXPath(string xpath)
    {
        XElement root = null;
        XElement parent = null;
        var splits = xpath.Split('/'); //split xpath into parts
        foreach (var split in splits)
        {
            var el = new XElement(split);
            if (parent != null)
                parent.Add(el);
            else
                root = el; //first element created, set as root
            parent = el;
        }
        return root;
    }
    

    Sample usage:

    var element = MakeFromXPath("My/Path/To/Element")'

    element will contain the value:

    <My>
      <Path>
        <To>
          <Element></Element>
        </To>
      </Path>
    </My>
    
    0 讨论(0)
提交回复
热议问题