我们如何将字符串内容传递给下面的“比较器”函数?
public static void Sort(XmlNodeList nodes, Comparison<XmlElement> comparer)
{
// The nodes.Count == 0 will break the nodes[0].ParentNode,
// the nodes.Count == 1 is pure optimization :-)
if (nodes.Count < 2)
{
return;
}
var parent = nodes[0].ParentNode;
var list = new List<XmlElement>(nodes.Count);
foreach (XmlElement element in nodes)
{
list.Add(element);
}
list.Sort(Comparer);
foreach (XmlElement element in list)
{
// You can't remove in the other foreach, because it will break
// the childNodes collection
parent.RemoveChild(element);
parent.AppendChild(element);
}
}
public static int Comparer(XmlElement a, XmlElement b,str strAttributeName)
{
int aaa = int.Parse(a.Attributes["aa"].Value);
int aab = int.Parse(b.Attributes["aa"].Value);
int cmp = aaa.CompareTo(aab);
if (cmp != 0)
{
return cmp;
}
int ba = int.Parse(a.Attributes["b"].Value);
int bb = int.Parse(b.Attributes["b"].Value);
cmp = ba.CompareTo(bb);
return cmp;
}
在这里,我想像上面的代码a.Attributes["aa"].Value
一样a.Attributes[strAttributeName].Value
使其更通用。我们该怎么做呢?
请帮忙。