读取XML并将其存储在SQL Server中。获取重复项
问题描述:
我正在尝试从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();
}
}
}
问题是,虽然作业是唯一的,我在我的表中得到许多重复。每次运行程序时,重复都会随机编号。哪里出错?
答
该网页绝对有重复。我用下面的代码验证。该网页似乎没有很好地形成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);
}
}
}
}
}
}
答
您似乎认为结果在解析结果时不会改变,但情况可能并非如此。如果有新帖子出现,它可能会出现在列表的开头,并将其余结果推下一个。这会导致页面上的最后一个项目在下一页上被复制。
此外,您正在查询的查询似乎没有确切的顺序。当您搜索时,现有的结果可能会改变顺序。同样,如果项目在搜索中转移,则可能会导致重复或跳过项目。
使用webbrowser转到url。您只使用职位,公司通常在同一个职位上发布很多职位。使用jobkey为每个发布获取唯一编号。 – jdweng
我没有在我的问题中显示所有标签。其中一个标签是,并且是唯一的。事实上,网站为每项工作分配一个唯一的密钥。但是在我的表格中,我看到许多重复的工作密钥相同。 –
Iman