2013-07-18 17 views
2

我想從的foreach如何在XmlNodeList的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(); 
     } 
    } 

有任何想法嗎?提前致謝。

回答

3

改爲使用for循環,該循環從最後一個元素開始,並返回第一個元素。

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

更新:我添加了一個if-block,以確保只有在集合中有多個元素時纔會運行循環。

+0

其工作...感謝您的寶貴答覆 – user2500094

-1

這可以幫助你:

公共靜態字符串ReverseString(字符串s){ 焦炭 [] = ARR s.ToCharArray(); Array.Reverse(arr); 返回新字符串(arr); }

+1

OP不處理字符串或數組。他有一個XmlNodeList,它沒有ToArray()方法。另外,編寫一個只調用另一個方法的方法似乎是爲了這個目的而矯枉過正。 –