我有一个包含XML字符串的列表.如何根据其中一个XML属性对该列表进行排序
本文关键字:列表 XML 属性 一个 排序 字符串 包含 有一个 何根 | 更新日期: 2023-09-27 18:26:55
例如:
该列表定义为:
List<string> listOfXML = new List<string>();
如果该列表包含下面给出的3个XML字符串,我如何根据"ThirdAttribute"对该列表进行排序?
XML字符串1:
<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>3</ThirdAttribute>
</Element>
XML字符串2:
<Element>
<FirstAttribute>C</FirstAttribute>>
<SecondAttribute>D</SecondAttribute>
<ThirdAttribute>4</ThirdAttribute>
</Element>
XML字符串3:
<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>1</ThirdAttribute>
</Element>
排序后的列表应具有以下顺序的元素:
<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>1</ThirdAttribute>
</Element>
<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>3</ThirdAttribute>
</Element>
<Element>
<FirstAttribute>C</FirstAttribute>>
<SecondAttribute>D</SecondAttribute>
<ThirdAttribute>4</ThirdAttribute>
</Element>
使用linq到xml可以做到这一点:
orderedList = listOfXml.OrderBy(x =>(int)XElement.Parse(x).Element("ThirdAttribute"))
public class XmlAttributesIntComparer : IComparer<string>
{
private readonly string elementName;
public XmlAttributesIntComparer(string elementName)
{
this.elementName = elementName;
}
public int Compare(string x, string y)
{
XmlDocument doc1 = new XmlDocument();
doc1.LoadXml(x);
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(y);
XmlText text1 = (XmlText)doc1.DocumentElement[elementName].FirstChild;
XmlText text2 = (XmlText)doc2.DocumentElement[elementName].FirstChild;
int attr1 = Convert.ToInt32(text1.Value);
int attr2 = Convert.ToInt32(text2.Value);
return attr1.CompareTo(attr2);
}
}
// ...
listOfXML.Sort(new XmlAttributesIntComparer("ThirdAttribute"));
Denis有更好的答案
一个非常非常糟糕的解决方案,但一个解决方案。。。
List<string> listOfXML = new List<string>()
{
@"<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>3</ThirdAttribute>
</Element>",
@"<Element>
<FirstAttribute>C</FirstAttribute>>
<SecondAttribute>D</SecondAttribute>
<ThirdAttribute>4</ThirdAttribute>
</Element>",
@"<Element>
<FirstAttribute>A</FirstAttribute>>
<SecondAttribute>B</SecondAttribute>
<ThirdAttribute>1</ThirdAttribute>
</Element>"
};
List<string> order = new List<string>();
int[] orders = new int[listOfXML.Count];
int arraylength = orders.Length;
for (int i = 0; i < arraylength; i++)
{
orders[i] = Convert.ToInt32(XElement.Parse(listOfXML[i]).Element("ThirdAttribute").Value);
}
Array.Sort(orders);
for (int i = 0; i < arraylength; i++)
{
string orderedXMLString = GetXmlString(orders[i], listOfXML);
order.Add(orderedXMLString);
}
private static string GetXmlString(int p, List<string> listOfXML)
{
string retXML = string.Empty;
foreach (var item in listOfXML)
{
if (p == Convert.ToInt32(XElement.Parse(item).Element("ThirdAttribute").Value))
{
retXML = item;
}
}
return retXML;
}
更好的方法是将所有元素组合成一个字符串。将组合字符串读取为XML,然后使用LINQ进行排序。将排序后的XML中的每个元素添加到列表中。
另一种方法是尝试在LINQ中使用let关键字读取"ThirdAttribute"值,然后以某种方式进行相应排序:
var orderedXML = from elem in listOfXML
let o = (XElement.Parse(elem).Element("ThirdAttribute").Value)
select new elem and orderby here;
我一开始尝试过,但无法超越!!!所以就想出了上面这个糟糕的解决方案。