C#-XML-比较两个节点是否相等
代码
1 public bool compareNode(XmlNode node1, XmlNode node2)
2 {
3 XmlNodeList node1ChildNodes = node1.ChildNodes;
4 XmlNodeList node2ChildNodes = node2.ChildNodes;
5 XmlAttributeCollection node1Attributes = node1.Attributes;
6 XmlAttributeCollection node2Attributes = node2.Attributes;
7
8 if (node1.Name == node2.Name && node1ChildNodes.Count == node2ChildNodes.Count)
9 {
10 if (node1Attributes == null && node2Attributes != null || node1Attributes != null && node2Attributes == null)
11 {
12 return false;
13 }
14 else if (node1Attributes != null && node2Attributes != null)
15 {
16 if (node1Attributes.Count == node2Attributes.Count)
17 {
18 int m = 0;
19 if (node1Attributes.Count > 0)
20 {
21 while (m < node1Attributes.Count && node1Attributes.Item(m).Name == node2Attributes.Item(m).Name && node1Attributes.Item(m).Value == node2Attributes.Item(m).Value)
22 {
23 m++;
24 }
25 if (m < node1Attributes.Count)
26 {
27 return false;
28 }
29 }
30 }
31 else
32 {
33 return false;
34 }
35 }
36 if (node1ChildNodes.Count == 0)
37 {
38 if (node1.InnerText != node2.InnerText)
39 {
40 return false;
41 }
42 }
43 else
44 {
45 int k = 0;
46 while (k < node1ChildNodes.Count && compareNode(node1ChildNodes.Item(k), node2ChildNodes.Item(k)))
47 {
48 k++;
49 }
50 if (k < node1ChildNodes.Count)
51 {
52 return false;
53 }
54 }
55 return true;
56 }
57 else
58 {
59 return false;
60 }
61 }
2 {
3 XmlNodeList node1ChildNodes = node1.ChildNodes;
4 XmlNodeList node2ChildNodes = node2.ChildNodes;
5 XmlAttributeCollection node1Attributes = node1.Attributes;
6 XmlAttributeCollection node2Attributes = node2.Attributes;
7
8 if (node1.Name == node2.Name && node1ChildNodes.Count == node2ChildNodes.Count)
9 {
10 if (node1Attributes == null && node2Attributes != null || node1Attributes != null && node2Attributes == null)
11 {
12 return false;
13 }
14 else if (node1Attributes != null && node2Attributes != null)
15 {
16 if (node1Attributes.Count == node2Attributes.Count)
17 {
18 int m = 0;
19 if (node1Attributes.Count > 0)
20 {
21 while (m < node1Attributes.Count && node1Attributes.Item(m).Name == node2Attributes.Item(m).Name && node1Attributes.Item(m).Value == node2Attributes.Item(m).Value)
22 {
23 m++;
24 }
25 if (m < node1Attributes.Count)
26 {
27 return false;
28 }
29 }
30 }
31 else
32 {
33 return false;
34 }
35 }
36 if (node1ChildNodes.Count == 0)
37 {
38 if (node1.InnerText != node2.InnerText)
39 {
40 return false;
41 }
42 }
43 else
44 {
45 int k = 0;
46 while (k < node1ChildNodes.Count && compareNode(node1ChildNodes.Item(k), node2ChildNodes.Item(k)))
47 {
48 k++;
49 }
50 if (k < node1ChildNodes.Count)
51 {
52 return false;
53 }
54 }
55 return true;
56 }
57 else
58 {
59 return false;
60 }
61 }