Question

I have below code in c#, where I am trying to generate 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();
}

How can I sorting xml rendering on the basis of "order" (highlighted above) attribute value, I am not going to use XSLT, however LINQ is fine.

Please suggest!!

Thanks

Was it helpful?

Solution 2

Sort your list before calling Generatexml() :

foreach (Component compCategories in categories.OrderBy(c => c.Title).ToList())
{
    GenerateXML(xml, compCategories);
}

OTHER TIPS

Could you sort your collection before generating the XML?

var ordered = categories.OrderBy(cat=>cat.Title);
foreach (Component compCategories in ordered)
{
    GenerateXML(xml, compCategories);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top