Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将多个.xml文件导入到数据集中_C#_Xml_Dataset_Directory - Fatal编程技术网

C# 将多个.xml文件导入到数据集中

C# 将多个.xml文件导入到数据集中,c#,xml,dataset,directory,C#,Xml,Dataset,Directory,所以,我想做的是: 我想将文件夹中的所有xml文件(比如说C:\Bla\AllMyLittleXmlFiles)导入到数据集中,对其进行填充,然后从那里将其导出到SQL Server。可能吗?似乎每次成功读取文件后,数据集都会自动清除,只留下第一个文件的数据 这是我的代码,包括一些不必要的东西: StringBuilder fileNames = new StringBuilder(); ArrayList filePaths = new ArrayList();

所以,我想做的是:

我想将文件夹中的所有xml文件(比如说
C:\Bla\AllMyLittleXmlFiles
)导入到数据集中,对其进行填充,然后从那里将其导出到SQL Server。可能吗?似乎每次成功读取文件后,数据集都会自动清除,只留下第一个文件的数据

这是我的代码,包括一些不必要的东西:

        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中的数据集从未实例化,所以。。。回到第二步。

试试这个:

DataSet ds = new Dataset();

for(int x=0;x<Filepath.Count;x++)
{
    ds.ReadXml(Filepath[x].ToString());
}
DataSet ds=新数据集();
对于(int x=0;x请尝试以下方法:

DataSet ds = new Dataset();

for(int x=0;x<Filepath.Count;x++)
{
    ds.ReadXml(Filepath[x].ToString());
}
DataSet ds=新数据集();

对于(int x=0;x也许您可以创建主数据集,并在将xml读入临时数据集后,尝试如下操作:

mainDataSet.Merge(tempDataSet);

也许您可以创建主数据集,并在将xml读入临时数据集后,尝试如下操作:

mainDataSet.Merge(tempDataSet);

这将解决你的问题,虽然不确定它是否有效

DataSet[] aDS = new DataSet[filePaths.Count]; 
StringBuilder uh = new StringBuilder();
int filesImported = 0;
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
//every xml file gets its own dataset
//so that new read operation will not clear data
aDS[filesImported].ReadXml(ob.ToString()); 
filesImported++;

}
int tablesimported = 0;
foreach (DataSet ds in aDS)
{
foreach (DataTable table in ds.Tables)
{
dataGridView1.DataSource = table.DefaultView;
tablesimported++;

}
}

这将解决你的问题,虽然不确定它是否有效

DataSet[] aDS = new DataSet[filePaths.Count]; 
StringBuilder uh = new StringBuilder();
int filesImported = 0;
foreach (object ob in filePaths)
{
string test = ob.ToString();
uh.Append(test);
//every xml file gets its own dataset
//so that new read operation will not clear data
aDS[filesImported].ReadXml(ob.ToString()); 
filesImported++;

}
int tablesimported = 0;
foreach (DataSet ds in aDS)
{
foreach (DataTable table in ds.Tables)
{
dataGridView1.DataSource = table.DefaultView;
tablesimported++;

}
}
试试这个:

        foreach (object ob in filePaths)
        {
            string test = ob.ToString();
            uh.Append(test);
            DataSet tmpDS = new DataSet();
            tmpDS.ReadXml(ob.ToString());
            aDS.Merge(tmpDS);
            filesImported++;
        }
这将给aDS留下每个文件的数据集表,这似乎就是您要寻找的

EDIT:[向@Reniuz致歉,他给你指出了完全相同的方向,但提前了20分钟!]试试这个:

        foreach (object ob in filePaths)
        {
            string test = ob.ToString();
            uh.Append(test);
            DataSet tmpDS = new DataSet();
            tmpDS.ReadXml(ob.ToString());
            aDS.Merge(tmpDS);
            filesImported++;
        }
这将给aDS留下每个文件的数据集表,这似乎就是您要寻找的


EDIT:[向@Reniuz致歉,他给你指出了完全相同的方向,但提前了20分钟!]我臃肿而低效的解决方案……我很失望

感谢Niko和Reniuz为我指明了正确的方向

    private ArrayList GetFilePaths()
    {
        ArrayList filePaths = new ArrayList();
        FolderBrowserDialog folder = new FolderBrowserDialog();
        folder.ShowDialog();

        foreach (string filePath in Directory.EnumerateFiles(folder.SelectedPath))
        {
            filePaths.Add(filePath.ToString());
        }
        return filePaths;
    }

    private void ImportXmls(ArrayList filePaths)
    {
        DataSet[] tempDSCollection = new DataSet[filePaths.Count];
        int impFiles = 0;
        foreach (object ob in filePaths)
        {
            DataSet impDS = new DataSet();
            impDS.ReadXml(ob.ToString());

            tempDSCollection[impFiles] = impDS;
            impFiles++;
        }

        foreach (DataSet aDS in tempDSCollection)
        {
            foreach (DataTable table in aDS.Tables)
            {
                mainDS.Merge(table);
            }
        } 
    }

我会继续努力并更新它,但现在还得做我臃肿低效的解决方案……我很失望

感谢Niko和Reniuz为我指明了正确的方向

    private ArrayList GetFilePaths()
    {
        ArrayList filePaths = new ArrayList();
        FolderBrowserDialog folder = new FolderBrowserDialog();
        folder.ShowDialog();

        foreach (string filePath in Directory.EnumerateFiles(folder.SelectedPath))
        {
            filePaths.Add(filePath.ToString());
        }
        return filePaths;
    }

    private void ImportXmls(ArrayList filePaths)
    {
        DataSet[] tempDSCollection = new DataSet[filePaths.Count];
        int impFiles = 0;
        foreach (object ob in filePaths)
        {
            DataSet impDS = new DataSet();
            impDS.ReadXml(ob.ToString());

            tempDSCollection[impFiles] = impDS;
            impFiles++;
        }

        foreach (DataSet aDS in tempDSCollection)
        {
            foreach (DataTable table in aDS.Tables)
            {
                mainDS.Merge(table);
            }
        } 
    }

我会继续研究并更新它,但现在必须做相同的问题,不同的迭代方式。相同的问题,不同的迭代方式。