【发布时间】:2011-09-28 19:56:59
【问题描述】:
所以,这就是我想做的:
我想将文件夹中的所有 xml 文件(比如说C:\Bla\AllMyLittleXmlFiles)导入数据集,对其进行处理,然后从那里将其导出到 SQL Server。那可能吗?似乎 DataSet 在每次成功读取文件后都会自行清除,只留下第一个文件的数据。
这是我的代码,包括一些不必要的东西:
StringBuilder fileNames = new StringBuilder();
ArrayList filePaths = new ArrayList();
FolderBrowserDialog folder = new FolderBrowserDialog();
folder.ShowDialog();
int pathLength = folder.SelectedPath.Length;
foreach (string file in Directory.EnumerateFiles(folder.SelectedPath))
{
string fielname = file.ToString().Substring(pathLength + 1);
string filepath = file.ToString();
fileNames.AppendLine(fielname);
filePaths.Add(filepath);
}
// textBox1.Text = filePaths[0].ToString();
DataSet aDS = new DataSet();
StringBuilder uh = new StringBuilder();
int filesImported = 0;
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
aDS.ReadXml(ob.ToString());
filesImported++;
}
int tablesimported = 0;
foreach (DataTable table in aDS.Tables)
{
dataGridView1.DataSource = table.DefaultView;
tablesimported++;
}
MessageBox.Show("Files Imported:" + filesImported.ToString() + " Tables Imported : " + tablesimported.ToString());
textBox1.Text = uh.ToString();
编辑 在尝试了一些答案后,我得到了这个:
int filesImported = 0;
foreach (object ob in filePaths)
{
dsCollection[filesImported].ReadXml(ob.ToString());
filesImported++;
}
int tablesImported = 0;
foreach (DataSet ds in dsCollection)
{
foreach (DataTable table in ds.Tables)
{
mainDS.Merge(table);
tablesImported++;
}
}
然后我在 dsCollection 上调用 Merge 方法。唯一的问题是 dscollection 中的数据集永远不会被实例化,所以......回到方块 2。
【问题讨论】: