2016-03-02 24 views
1

我正在嘗試從URL中讀取XML Feed並將其存儲在數據庫中。 XML格式如下所示:讀取XML並將其存儲在SQL Server中。獲取重複項

<response version="2"> 
    <totalresults>1249943</totalresults> 
    <results> 
    <result> 
     <jobtitle>Call Center </jobtitle> 
     <company>CVS Health</company> 
     <city>Work at Home</city> 
    </result> 

    <result> 
     <jobtitle>Java Programmer</jobtitle> 
     <company>Jonah Group</company> 
     <city>Toronto</city> 
    </result> 
    </results> 
</response> 

而我試圖爲所有工作存儲職位,公司和城市。有數百萬個工作。這裏是我在C#代碼#

public override void getJobsFromSource() 
{ 
    string url = @"http://api.indeed.com/ads/apisearch?publisher=5566998848654317&v=2&q=%22%22&filter=1%22%22&limit=25"; 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(url); 
    int totalResults = int.Parse(doc.SelectSingleNode("response /totalresults").InnerText); 

    for (int i = 0; i < totalResults; i += 25) 
    { 
     string newUrl = [email protected]"http://api.indeed.com/ads/apisearch?publisher=5566998848654317&v=2&q=%22%22&filter=1&limit=25&start={i}"; 
     doc.Load(newUrl); 
     DataSet ds = new DataSet(); 
     XmlNodeReader xmlReader = new XmlNodeReader(doc); 

     while (xmlReader.ReadToFollowing("results")) 
     { 
      ds.ReadXml(xmlReader); 
     } 

     if (ds.Tables.Count > 0) 
     { 
      SqlConnection con = new SqlConnection(); 
      con.ConnectionString = "data source=10.0.0.76;initial catalog=JobSearchDB;persist security info=True;user id=sa;password=bonddbl07;MultipleActiveResultSets=True;App=EntityFramework"; 
      con.Open(); 

      SqlBulkCopy sbc = new SqlBulkCopy(con); 
      sbc.DestinationTableName = "IndeedJob"; 

      sbc.ColumnMappings.Clear(); 
      sbc.ColumnMappings.Add("jobtitle", "jobtitle"); 
      sbc.ColumnMappings.Add("company", "company"); 
      sbc.ColumnMappings.Add("city", "city"); 
      sbc.WriteToServer(ds.Tables[0]); 
      con.Close(); 
     } 
    } 
} 

問題是,雖然作業是唯一的,我在我的表中得到許多重複。每次運行程序時,重複都會隨機編號。哪裏出錯?

+0

使用webbrowser轉到url。您只使用職位,公司通常在同一個職位上發佈很多職位。使用jobkey爲每個發佈獲取唯一編號。 – jdweng

+0

我沒有在我的問題中顯示所有標籤。其中一個標籤是,並且是唯一的。事實上,網站爲每項工作分配一個唯一的密鑰。但是在我的表格中,我看到許多重複的工作密鑰相同。 – Iman

回答

2

該網頁絕對有重複。我用下面的代碼驗證。該網頁似乎沒有很好地形成XML,所以我不得不修改你的代碼,以便能夠閱讀網頁。使用Linq我能夠刪除重複項。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 
using System.Xml.Schema; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     DataSet ds = new DataSet("Jobs"); 
     public Form1() 
     { 
      InitializeComponent(); 
      getJobsFromSource(); 
      DataTable dt = ds.Tables[0]; 
      dt = dt.AsEnumerable().GroupBy(x => x.Field <string>("jobkey")).Select(x => x.FirstOrDefault()).OrderBy(y => y.Field<string>("jobkey")).CopyToDataTable(); 
      dataGridView1.DataSource = dt; 
     } 
     public void getJobsFromSource() 
{ 
      string url = @"http://api.indeed.com/ads/apisearch?publisher=5566998848654317&v=2&q=%22%22&filter=1%22%22&limit=25"; 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(url); 
      int totalResults = int.Parse(doc.SelectSingleNode("response /totalresults").InnerText); 
      for (int i = 0; i < totalResults; i += 25) 
      { 
       string newUrl = @"http://api.indeed.com/ads/apisearch?publisher=5566998848654317&v=2&q=%22%22&filter=1&limit=25&start={i}"; 

       XmlReaderSettings settings = new XmlReaderSettings(); 
       settings.ValidationType = ValidationType.None; 
       settings.IgnoreWhitespace = true; 
       XmlReader xmlReader = XmlReader.Create(newUrl, settings); 

       while (!xmlReader.EOF) 
       { 
        if (xmlReader.Name != "result") 
        { 
         xmlReader.ReadToFollowing("result"); 
        } 
        if(!xmlReader.EOF) 
        { 
         ds.ReadXml(xmlReader); 
        } 
       } 
      } 
     } 
    } 
} 
0

您似乎認爲結果在解析結果時不會改變,但情況可能並非如此。如果有新帖子出現,它可能會出現在列表的開頭,並將其餘結果推下一個。這會導致頁面上的最後一個項目在下一頁上被複制。

此外,您正在查詢的查詢似乎沒有確切的順序。當您搜索時,現有的結果可能會改變順序。同樣,如果項目在搜索中轉移,則可能會導致重複或跳過項目。

相關問題