下面是一些代码,这些代码可以处理一批多个XML文件,并将它们添加到数据集中的自己的数据表中:
private void Btn_Import_Click(object sender, RoutedEventArgs e)
{
/*ILRChecks.ILRReport.NullChecks();
ILRChecks.ILRReport.CrossYear();*/
DataSet dataSet = new DataSet();
DataTable diff = new DataTable();
diff.TableName = "Differences";
diff.Columns.Add("LearnRefNumber");
diff.Columns.Add("ULN");
//diff.Columns.Add("FamilyName");
//diff.Columns.Add("GivenNames");
diff.Columns.Add("DateofBirth");
diff.Columns.Add("NINumber");
diff.Columns.Add("Message");
Dictionary<int,DataTable> Tables = new Dictionary<int,DataTable>();
foreach (string str_FileLocation in Global.fileNames)
{
FileInfo fileInfo = new FileInfo(str_FileLocation);
string str_xmlFileName = fileInfo.Name;
XDocument doc = XDocument.Load(str_FileLocation);
var ns = doc.Root.GetDefaultNamespace();
var learner = doc.Descendants(ns + "Learner");
string shortfile = str_xmlFileName.Substring(13,4);
StringReader reader = new StringReader(new XElement("Sample", learner).ToString());
DataSet ds = new DataSet();
ds.ReadXml(reader);
Tables.Add(int.Parse(shortfile), ds.Tables[0]);
DataTable dt = ds.Tables[0].Copy();
dt.TableName = shortfile;
dataSet.Tables.Add(dt);
}
然后,从这里开始,我希望能够依次遍历这些数据集,并按数据表名称的顺序对它们进行比较。名称已被转换为Integers(整数),需要从最近的年份开始,因此取决于文件的数量,分别是1819、1718等。 我相信我有一条有效的foreach语句,允许循环获取正确的表顺序并将其放入当前项目表中:
foreach (var item in Tables.OrderByDescending(k => k.Key))
{
if (item.Key != Tables.OrderByDescending(k => k.Key).Last().Key)
{
DataTable dataTable_CurrentItem = item.Value;
}
}
然后从这里开始,我有一部分代码可以独立运行,但是我需要将其作为循环的一部分并入上面,在进行过程中比较文件,但我怀疑这部分内容并不完全对,以及是否应该在上面的foreach语句中使用,我不确定:
var diff_ULNDOB = from Latest in dataSet.Tables["1819"].AsEnumerable()
join previous in dataSet.Tables["1718"].AsEnumerable()
on Latest.Field<string>("ULN") equals previous.Field<string>("ULN")
where Latest.Field<string>("DateofBirth") != previous.Field<string>("DateofBirth")
select new { ULN = Latest.Field<string>("ULN"), FamilyName = Latest.Field<string>("FamilyName") };
if (diff_ULNDOB.Any())
{.
MessageBox.Show("PROBLEM!!!!!!");
}
答案 0 :(得分:1)
请参阅下面的更改。您不需要使用DataSet。您可以只使用DataTable。 :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Threading.Tasks;
namespace ConsoleApplication100
{
class Program
{
static void Main(string[] args)
{
}
}
public class test
{
private void Btn_Import_Click(object sender, RoutedEventArgs e)
{
/*ILRChecks.ILRReport.NullChecks();
ILRChecks.ILRReport.CrossYear();*/
DataSet dataSet = new DataSet();
DataTable diff = new DataTable();
diff.TableName = "Differences";
diff.Columns.Add("LearnRefNumber");
diff.Columns.Add("ULN");
//diff.Columns.Add("FamilyName");
//diff.Columns.Add("GivenNames");
diff.Columns.Add("DateofBirth");
diff.Columns.Add("NINumber");
diff.Columns.Add("Message");
Dictionary<int, DataTable> Tables = new Dictionary<int, DataTable>();
foreach (string str_FileLocation in Global.fileNames)
{
FileInfo fileInfo = new FileInfo(str_FileLocation);
string str_xmlFileName = fileInfo.Name;
XDocument doc = XDocument.Load(str_FileLocation);
var ns = doc.Root.GetDefaultNamespace();
var learner = doc.Descendants(ns + "Learner");
string shortfile = str_xmlFileName.Substring(13, 4);
StringReader reader = new StringReader(new XElement("Sample", learner).ToString());
DataTable dt = new DataTable();
dt.ReadXml(reader);
Tables.Add(int.Parse(shortfile), dt);
}
var orderTables = Tables.OrderByDescending(k => k.Key).ToList();
for (int i = 1; i <= orderTables.Count(); i--)
{
var diff_ULNDOB = from Latest in orderTables[i].AsEnumerable()
join previous in orderTables[i - 1].AsEnumerable()
on Latest.Field<string>("ULN") equals previous.Field<string>("ULN")
where Latest.Field<string>("DateofBirth") != previous.Field<string>("DateofBirth")
select new { ULN = Latest.Field<string>("ULN"), FamilyName = Latest.Field<string>("FamilyName") };
}
}
}
}