【发布时间】:2021-11-04 17:30:26
【问题描述】:
我正在将 100-500 个 XML 文件中的数据加载到 DataTable 以填充 Infragistics UltraGrid。这些文件小至 100K,大至 2MB。我一直在寻找加快加载时间并考虑异步的方法,但数据表不是线程安全的。
看流程,加载xdoc大约需要2/3的时间,另外1/3是在读取xdoc和向表中添加数据。
有没有办法使用异步并以某种方式加载下一个 XDocument 而前一个被读取并加载到数据表中?我看过,但我没有看到一个很好的方法来为每个人做到这一点。还有其他一些我应该考虑的策略吗?
这是我正在做的简化版本:
private void openXMLs(){
OpenFileDialog openFileDialog1 = new OpenFileDialog
{
Title = "Browse XML files",
CheckFileExists = true,
CheckPathExists = true,
DefaultExt = "xml",
Filter = @"XML files (*.xml)|*.xml",
FilterIndex = 2,
RestoreDirectory = true,
ReadOnlyChecked = true,
ShowReadOnly = true,
Multiselect = true
};
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
foreach (String file in openFileDialog1.FileNames)
{
XDocument xdoc = XDocument.Load(file)
loadData(xdoc)
}
}
}
private void loadData(XDocument xdoc){
var query = xdoc.Descendants("root").AsEnumerable()
.Select(c => new
{
id = c.Element("ID").Value,
firstname = c.Element("firstname").Value,
lastname = c.Element("lastname").Value,
state = c.Element("state").Value,
});
foreach (var item in query)
{
_dt.Rows.Add(
item.id,
item.firstname,
item.lastname,
item.state
);
}
}
【问题讨论】:
-
我正在将 100-500 个 XML 文件中的数据加载到 DataTable 中
-
很遗憾,这是我必须工作的环境。
标签: c# xml async-await parallel-processing