如何在c#中基于属性进行排序
本文关键字:属性 排序 于属性 | 更新日期: 2023-09-27 18:08:19
我在c#中有下面的代码,我试图生成xml。
foreach (Component compCategories in categories)
{
GenerateXML(xml, compCategories);
}
private void GenerateXML(XmlWriter xml, Component compCategory)
{
xml.WriteStartElement("category");
xml.WriteAttributeString("id", compCategory.Id.ItemId.ToString());
string order = compCategory.Title; xml.WriteAttributeString("order", order);
Component detailCategory = compCategory.ComponentValue("Detail");
if (detailCategory != null)
{
xml.WriteAttributeString("detail", detailCategory.Id.ItemId.ToString());
}
Component catArtwork = compCategory.ComponentValue("Artwork");
if (catArtwork != null)
{
Component summaryArtwork = catArtwork.ComponentValue("Summary");
if (summaryArtwork != null)
{
String CatImageUrl = PublishBinary(summaryArtwork);
xml.WriteAttributeString("image", CatImageUrl);
}
}
xml.WriteElementString("title", compCategory.StringValue("Title").ToString());
xml.WriteElementString("summary", compCategory.StringValue("Summary").ToString());
xml.WriteElementString("linktext", compCategory.StringValue("Linktext").ToString());
xml.WriteEndElement();
}
我如何根据"order"(上面突出显示的)属性值对xml呈现进行排序,我不打算使用XSLT,但是LINQ很好。
请建议! !
谢谢
您能在生成XML之前对集合进行排序吗?
var ordered = categories.OrderBy(cat=>cat.Title);
foreach (Component compCategories in ordered)
{
GenerateXML(xml, compCategories);
}
在调用Generatexml()之前对列表进行排序:
foreach (Component compCategories in categories.OrderBy(c => c.Title).ToList())
{
GenerateXML(xml, compCategories);
}