我在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());
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没问题。
请建议!!
由于
答案 0 :(得分:2)
您可以在生成XML之前对集合进行排序吗?
var ordered = categories.OrderBy(cat=>cat.Title);
foreach (Component compCategories in ordered)
{
GenerateXML(xml, compCategories);
}
答案 1 :(得分:1)
在调用Generatexml()之前对列表进行排序:
foreach (Component compCategories in categories.OrderBy(c => c.Title).ToList())
{
GenerateXML(xml, compCategories);
}