如何在XmlNodeList的foreach中使用reverse?

时间:2013-07-18 07:49:02

标签: c# asp.net xml-parsing foreach

我想从foreach

获取相反的数据

此处XmlNodeList comments = d.SelectNodes("//comments/comment/creator");

我有4个值,我想反转它

我写过这段代码

 public void commentM4(string pstId, string cmtId)
    {
        XmlDocument d = new XmlDocument();
        d.LoadXml(content);

        XmlNodeList comments = d.SelectNodes("//comments/comment/creator");
        foreach (XmlNode xncomment in  comments)
        {
            commentMemberId = xncomment["id"].InnerText;
            DbConnection.Open();
            DbCommand = new OleDbCommand("select count(response_id) from  mw_response where customer_id = '" + commentMemberId + "' AND post_id='" + pstId + "'", DbConnection);
            OleDbDataReader DbReader = DbCommand.ExecuteReader();

            while (DbReader.Read())
            {
                count = DbReader[0].ToString();
                cnt = Convert.ToInt32(count);
                if ((cnt == 0) && (comments1 != ""))
                {
                    DbCommand = new OleDbCommand("update mw_response set prod_id = (select prod_id from mw_post where post_id='" + pstId + "'),customer_id = (select customer_id from mw_customer where customer_id='" + commentMemberId + "') where response_id = '" + cmtId + "'", DbConnection);
                    DbCommand.ExecuteNonQuery();
                }
            }
            DbReader.Close();
            DbConnection.Close();
        }
    }

有什么想法吗?提前谢谢。

2 个答案:

答案 0 :(得分:4)

使用for循环,从最后一个元素开始,然后返回第一个元素。

if (comments.Count > 0)
{
    for (int i = (comments.Count - 1); i >= 0; i--)
    {
     XmlNode xncomment = comments[i];
    //Rest of the logic...
    }
}

更新:我添加了一个if-block,以确保只有在集合中有多个元素时才会运行循环。

答案 1 :(得分:-1)

这有助于您:

public static string ReverseString(string s)     {     char [] arr = s.ToCharArray();     Array.Reverse(ARR);     返回新字符串(arr);     }