2 using System.Collections;
7 public class ComparisonException : Exception
9 public ComparisonException (string message)
15 static bool debug = false;
16 static bool error = false;
18 public static void Main (string [] args)
20 if (args.Length < 2) {
21 Console.Error.WriteLine ("Usage: xmldocdiff [reference_output.xml] [actual_output.xml]");
24 if (args.Length > 2 && args [2].EndsWith ("-debug"))
29 } catch (Exception ex) {
30 Console.WriteLine ("FAIL: " + args [1]);
33 Console.WriteLine ("PASS: " + args [1]);
36 private static void Run (string [] args)
38 XmlDocument doc1 = new XmlDocument ();
40 XmlDocument doc2 = new XmlDocument ();
43 XmlNodeList memberList1 = doc1.SelectNodes ("/doc/members/member");
44 XmlNodeList memberList2 = doc2.SelectNodes ("/doc/members/member");
46 Hashtable namedItems = new Hashtable ();
48 foreach (XmlElement el in memberList1)
49 namedItems.Add (el.GetAttribute ("name"), el);
50 foreach (XmlElement el2 in memberList2) {
51 string name = el2.GetAttribute ("name");
52 XmlElement el1 = namedItems [name] as XmlElement;
54 Report ("Extraneous element found. Name is '{0}'", name);
57 namedItems.Remove (name);
59 CompareNodes (el1, el2);
62 foreach (string name in namedItems.Keys)
63 Report ("Expected comment was not found. Name is {0}, XML is {1}", name, ((XmlElement) namedItems [name]).OuterXml);
65 // finally, check other nodes than members
66 doc1.SelectSingleNode ("/doc/members").RemoveAll ();
67 doc2.SelectSingleNode ("/doc/members").RemoveAll ();
68 string xml1 = doc1.OuterXml.Replace ("\r", "").Trim ();
69 string xml2 = doc2.OuterXml.Replace ("\r", "").Trim ();
71 Report (@"Either of doc, assembly, name, members elements are different.
73 doc2: {1}", xml1, xml2);
76 private static void CompareNodes (XmlNode n1, XmlNode n2)
79 Report (@"Nodes does not exist:
80 Node1: {0}", n1.OuterXml);
83 if (n1.NodeType != n2.NodeType) {
84 Report (@"Nodes differ:
86 Node2: {1}", n1.OuterXml, n2.OuterXml);
89 if (n1.Name != n2.Name) {
90 Report (@"Node names differ:
92 Node2: {1}", n1.OuterXml, n2.OuterXml);
95 if (n1 is XmlElement) {
96 for (int i = 0; i < n1.Attributes.Count; i++)
97 CompareNodes (n1.Attributes [i],
99 for (int i = 0; i < n1.ChildNodes.Count; i++)
100 CompareNodes (n1.ChildNodes [i],
103 if (n1.NodeType != XmlNodeType.Comment && n1.Value != null) {
104 string v1 = n1.Value.Trim ().Replace ("\r", "");
105 string v2 = n2.Value.Trim ().Replace ("\r", "");
107 Report (@"Node values differ:
109 Node2: {1}", v1, v2);
113 static void Report (string format, params object [] args)
117 Console.WriteLine (format, args);
119 throw new ComparisonException (String.Format (format, args));