我们如何对XmlNodeList中的最后四项进行反向迭代

时间:2017-04-07 10:24:56

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

此时它以相反的顺序显示项目,没有限制。 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 = "";
}

2 个答案:

答案 0 :(得分:0)

您只需替换以下行:

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

由此:

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

答案 1 :(得分:0)

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 = "";
}