1

我正在尝试从 LinkedIn 获取数据,然后将其存储在数据库中。

我在循环中遇到了一些问题

1.在第一个 foreach 我插入数据。

2.将(user_id)传递给第二个插入语句。

3.第二次foreach插入数据,然后我想先foreach

我已经写了这段代码

 public void customerM()
    {
        XmlDocument d = new XmlDocument();
        d.LoadXml(content);
        XmlNodeList customer = d.SelectNodes("//posts/post/creator");
        foreach (XmlNode cus in customer)
        {
            user_id = cus["id"].InnerText;
            customerFirst_name = cus["first-name"].InnerText;
            customerLast_name = cus["last-name"].InnerText;
            customerName = string.Format("{0} {1}", customerFirst_name, customerLast_name);
            DbConnection.Open();
            DbCommand = new OleDbCommand("select count(*) from mw_customer where customer_id='" + user_id + "'", DbConnection);
            OleDbDataReader DbReader = DbCommand.ExecuteReader();
            while (DbReader.Read())
            {
                count = DbReader[0].ToString();
                cnt = Convert.ToInt32(count);
                if (cnt == 0)
                {
                    DbCommand = new OleDbCommand("insert into mw_customer(customer_id,name,first_name,last_name,user_id,cust_date,community) values('" + user_id + "','" + customerName + "','" + customerFirst_name + "','" + customerLast_name + "','" + user_id + "','" + post_timestamp + "','LinkedIn')", DbConnection);
                    DbCommand.ExecuteNonQuery();
                }
                else
                {
                    DbCommand = new OleDbCommand("UPDATE mw_customer set name='" + customerName + "',first_name='" + customerFirst_name + "',last_name='" + customerLast_name + "',user_id='" + user_id + "',community = 'LinkedIn',cust_date='" + post_timestamp + "' where customer_id = '" + user_id + "'", DbConnection);
                    DbCommand.ExecuteNonQuery();
                }
            }
            DbReader.Close();

            XmlNodeList title = d.SelectNodes("//posts/post");

            foreach (XmlNode post in title)
            {
                posts = post["title"].InnerText;
                post_id = post["id"].InnerText;
                string postTimeStamp = post["creation-timestamp"].InnerText;
                double postTimeStamp1 = Convert.ToDouble(postTimeStamp);
                DateTime post_timestamp1 = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(Math.Round(postTimeStamp1 / 1000d)).ToLocalTime();
                post_timestamp = post_timestamp1.ToString("dd-MMM-yy");

                DbCommand = new OleDbCommand("select count(*) from mw_post where post_id = '" + post_id + "'", DbConnection);
                OleDbDataReader DbReader1 = DbCommand.ExecuteReader();
                while (DbReader1.Read())
                {
                    count = DbReader1[0].ToString();
                    cnt = Convert.ToInt32(count);
                    if ((cnt == 0) && (posts != ""))
                    {
                        DbCommand = new OleDbCommand("insert into mw_post(post_id,customer_id,post,post_date,community) values('" + post_id + "','" + user_id + "','" + posts + "', '" + post_timestamp + "','LinkedIn')", DbConnection);
                        DbCommand.ExecuteNonQuery();

                        if (posts.ToUpper().Contains("Personal Loan".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '2',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else if (posts.ToUpper().Contains("Credit Card".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '1',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else if (posts.ToUpper().Contains("Home Loan".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '3',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else if (posts.ToUpper().Contains("Car Loan".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '4',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else if (posts.ToUpper().Contains("Deposit".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '5',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else if (posts.ToUpper().Contains("Debit Card".ToUpper()))
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '7',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                        else
                        {
                            DbCommand = new OleDbCommand("UPDATE mw_post set prod_id = '6',customer_id='" + user_id + "' where post = '" + posts + "'", DbConnection);
                            DbCommand.ExecuteNonQuery();
                        }
                    }
                }
                DbReader1.Close();
                DbConnection.Close();
            }
        }
    }

XML 文件

  <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n
  <posts total=\"1\">\n  <post>\n    
  <creation-timestamp>
  1362659632000
    </creation-timestamp>\n 
   <id>
      g-4891367-S-dfdsfd
   </id>\n    
   <title>
     dfsdfsdf
    </title>\n    
  <creator>\n
  <id>
      dfdsfsdf
   </id>\n      
  <first-name>
     dfsdfsd
    </first-name>\n     
      <last-name>
          dsfsdfd
    </last-name>\n      
     <picture-url>
         http://m3.licdn.com/mpr/mprx/0_zR-               8Kkl9rtYlGl1YcyCPK6tMrqDrGlCYBM60KFPQftdfsdfsdOJSfTn_tq4rYx8Svbl9K
     </picture-url>\n      
     <headline>
          dfsdfsd
    </headline>\n    
    </creator>\n 
    <comments total=\"1\">\n 
    <comment>\n 
    <creation-timestamp>
        1372061224000
    </creation-timestamp>\n
    <id>
       g-4891367-S-dsfsdfsd
    </id>\n        
    <text>
        dfsdfsd.
    </text>\n        
   <creator>\n          
   <id>
       C-dfsdfsd
   </id>\n          
   <first-name>
       dfsdfsd
   </first-name>\n  
   <last-name>
       dfsdfsd
   </last-name>\n         
   <headline>
       dsfsdsd
   </headline>\n   
   <picture-url>
              http://m3.licdn.com/mpr/mprx/0_OSEuUB4ZdfsdfUzsFvCLad5Y2yfY1Uqo4EXAO93Wutws8zNY9U75AECxh0metNPq6zdHm
   </picture-url>\n        
     </creator>\n        
     <creation-timestamp>
       1372061224895
    </creation-timestamp>\n        
   <relation-to-viewer>\n        
   <available-actions total=\"1\">\n
         <action>\n 
         <code>delete</code>\n        
         </action>\n          
    </available-actions>\n     
    </relation-to-viewer>\n     
   </comment>\n    
   </comments>\n   
    <likes total=\"2\">\n      
    <like>\n       
    <person>\n          
      <id>
         dfsfwe
      </id>\n          
     <first-name>
      dfsdfsd
   </first-name>\n         
  <last-name>
       dfsd
       </last-name>\n    
      <headline>
    dfsdf
       </headline>\n         
      <picture-url>
     http://m3.licdn.com/mpr/mprx/0dfsdfsdda7-q79MqA-X55lrEa7- tssMzKfRilTfaJfyrYdcvX3oeLmPSf_AzuRntGlAaCl
      </picture-url>\n   
     </person>\n 
    </like>\n  <like>\n        
       <person>\n          
      <id>
      S4Er-sdjfjd
  </id>\n          
    <first-name>
      fdsfs
     </first-name>\n         
      <last-name>
      dfsdd
       </last-name>\n          
    <headline>
     sdfsdfsdfds
     </headline>\n          
 <picture-url>
      http://m3.licdn.com/mpr/mprx/0_kdfsdfdsfsdo
 </picture-url>\n       
      </person>\n 
       </like>\n   </likes>\n    
       <relation-to-viewer>\n
      <is-following>true</is-following>\n 
       <is-liked>false</is-liked>\n    
      <available-actions total=\"6\">\n <action>\n  
 <code>add-comment</code>\n        
        </action>\n   <action>\n  
        <code>categorize-as-job</code>\n 
 </action>\n  <action>\n  
 <code>categorize-as-promotion</code>\n  
     </action>\n <action>\n  
 <code>delete</code>\n  
 </action>\n <action>\n  
 <code>like</code>\n  
 </action>\n  <action>\n   
 <code>unfollow</code>\n 
 </action>\n </available-actions>\n 
 </relation-to-viewer>\n  </post>\n</posts>\n"

有任何想法吗?提前致谢。

4

2 回答 2

1

看起来您正在循环一次"//posts/post/creator",然后再循环一次"//posts/post"。也许您刚刚反转了循环。尝试这个:

XmlNodeList title = d.SelectNodes("//posts/post");
foreach (XmlNode post in title)
{
    // Note: I only want creators of the current post
    XmlNodeList customer = post.SelectNodes("creator"); 
    foreach (XmlNode cus in customer)
    {
        // insert into mw_customer ... 

        // insert into mw_post ...
    }
}

附带说明,请查看使用 SQL 参数;它更安全,因为您当前的代码极易受到SQL 注入的影响。另外,我建议只进行一次插入,而不是在插入后进行多次更新。最后,使用IDENTITY列而不是通过 had with 创建您的密钥SELECT COUNT(*) ...

于 2013-07-02T06:15:09.427 回答
0

一旦第二个 foreach 插入数据,然后我又想先 foreach

由于第二个 foreach 循环嵌套在第一个 foreach 循环内,我假设您要重置第一个 foreach 循环。在这种情况下,for 循环(for (int i = 0; i < customer.Count; i++))可能会更好,只需将循环计数器重置为初始值。

这当然是一个快速修复,需要最少的重新编码。为了正确解决有关独立方法的建议,并在需要时调用它们甚至使用递归可能是解决此问题的更合适的方法。

于 2013-07-02T06:08:31.670 回答