Possible to iterate backwards through a foreach?

前端 未结 11 2127
无人及你
无人及你 2020-11-27 03:54

I know I could use a for statement and achieve the same effect, but can I loop backwards through a foreach loop in C#?

11条回答
  •  有刺的猬
    2020-11-27 04:32

    I have used this code which worked

                    if (element.HasAttributes) {
    
                        foreach(var attr in element.Attributes().Reverse())
                        {
    
                            if (depth > 1)
                            {
                                elements_upper_hierarchy_text = "";
                                foreach (var ancest  in element.Ancestors().Reverse())
                                {
                                    elements_upper_hierarchy_text += ancest.Name + "_";
                                }// foreach(var ancest  in element.Ancestors())
    
                            }//if (depth > 1)
                            xml_taglist_report += " " + depth  + " " + elements_upper_hierarchy_text+ element.Name + "_" + attr.Name +"(" + attr.Name +")" + "   =   " + attr.Value + "\r\n";
                        }// foreach(var attr in element.Attributes().Reverse())
    
                    }// if (element.HasAttributes) {
    

提交回复
热议问题