【问题标题】:how can we do reverse iteration of last four items in the XmlNodeList我们如何对 XmlNodeList 中的最后四个项目进行反向迭代
【发布时间】:2017-04-07 10:24:55
【问题描述】:

此时它正在以相反的顺序显示项目,没有限制。 XmlNodeList 可以有很多项。我只想显示最后四个项目。如何查找或显示列表中的最后四个项目?任何人?

XmlNodeList MyTestList = MyRssDocument.SelectNodes("test/holder/item");

string Title = "";
string Link = "";


for (int i = MyTestList.Count - 1; i >= 0; i--)
{
    XmlNode MyTestDetail;

    MyTestDetail = MyTestList.Item(i).SelectSingleNode("title");
    if (MyTestDetail != null)
        Title = MyTestDetail.InnerText;
    else
        Title = "";

    MyTestDetail = MyTestList.Item(i).SelectSingleNode("link");
    if (MyTestDetail != null)
        Link = MyTestDetail.InnerText;
    else
        Link = "";
}

【问题讨论】:

    标签: c# asp.net .net loops c#-4.0


    【解决方案1】:

    您可以只替换以下行:

    for (int i = MyTestList.Count - 1; i >= 0; i--)
    

    通过这个:

    for (int i = MyTestList.Count - 1; i >= (MyTestList.Count - 4) ; i--)
    

    【讨论】:

      【解决方案2】:
      for (int i = MyTestList.Count; i >= 4; i--)
      {
          XmlNode MyTestDetail;
      
          MyTestDetail = MyTestList.Item(i).SelectSingleNode("title");
          if (MyTestDetail != null)
              Title = MyTestDetail.InnerText;
          else
              Title = "";
      
          MyTestDetail = MyTestList.Item(i).SelectSingleNode("link");
          if (MyTestDetail != null)
              Link = MyTestDetail.InnerText;
          else
              Link = "";
      }
      

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 2021-01-19
        • 1970-01-01
        • 2013-07-17
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        相关资源
        最近更新 更多