C# 在数据集数组和System.NullReferenceException中保存XML文件选择多个XML文件时会发生异常

C# 在数据集数组和System.NullReferenceException中保存XML文件选择多个XML文件时会发生异常,c#,arrays,xml,dataset,C#,Arrays,Xml,Dataset,我编写了一个代码,其中我选择了几个XML文件并将它们保存在数据集数组中。因此,如果我选择了5个XMl文件,数据集数组大小为5(0-4),数据集中的表将填充XMl节点和子节点 代码的第一部分显示了我将XML数据保存到DataSet数组中的方式 这是代码的一部分 public partial Form1:Form { XmlReader xmlFile; DataSet[] XMlTable; string[] FileNames; string[] File;

我编写了一个代码,其中我选择了几个XML文件并将它们保存在数据集数组中。因此,如果我选择了5个XMl文件,数据集数组大小为5(0-4),数据集中的表将填充XMl节点和子节点

代码的第一部分显示了我将XML数据保存到DataSet数组中的方式

这是代码的一部分

public partial Form1:Form
{
    XmlReader xmlFile;

    DataSet[] XMlTable;

    string[] FileNames;
    string[] File;
。 .

private void Open\u File\u Btn\u单击(对象发送方,事件参数e)
{
OpenFileDialog openXmlFile=新建OpenFileDialog();
//Multiselect允许openfiledialog选择多个文件
openXmlFile.Multiselect=true;
openXmlFile.Filter=“XML文件|*.XML”;
openXmlFile.Title=“选择XML文件”;
整数大小;
dataGridView1.Anchor=(AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Top);
尝试
{
if(openXmlFile.ShowDialog()==DialogResult.OK)
{
if(!string.Equals(Path.GetExtension(openXmlFile.FileName),“.xml”,StringComparison.OrdinalIgnoreCase))
{
//选择的文件类型无效;显示错误
MessageBox.Show(“此应用程序不支持所选文件。您必须选择一个XML文件。”,“无效文件类型”,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
其他的
{
//找到一种只显示选定文件名的方法-现在它显示文件的整个地址
this.textBox1.Text=openXmlFile.FileName;
FileNames=openXmlFile.FileNames;
File=openXmlFile.safefilename;
大小=文件名。长度;
对于(int FileNumber=0;FileNumber
下面的代码是System.NullReferencesException错误发生的地方,它仅在我选择多个XML文件时发生

private void test1_Click(object sender, EventArgs e)
{
//Shows how big is the size of the Dataset array - it should be the same as the amount of selected files (5 files selected then array size = 5 (0-4)
        MessageBox.Show(Convert.ToString(string.Format("Size of Array ={0}", XMlTable.Length)));

        for (int test1 = 0; test1 < XMlTable.Length; test1++)
        {
            MessageBox.Show(Convert.ToString(string.Format("Tables used = {0}",XMlTable[test1].Tables.Count)));
            MessageBox.Show(XMlTable[test1].Tables[0].TableName);
        }
}
private void test1\u单击(对象发送方,事件参数e)
{
//显示数据集数组的大小-它应该与所选文件的数量相同(选择5个文件,然后数组大小=5(0-4)
Show(Convert.ToString(string.Format(“数组大小={0}”,XMlTable.Length));
for(int test1=0;test1
您的问题是每次迭代都要重新初始化
XMlTable

因此,只有数组中的最后一个数据集实际存在,其余数据集为空。
XMlTable=newdataset[size];
移动到循环之前

private void test1_Click(object sender, EventArgs e)
{
//Shows how big is the size of the Dataset array - it should be the same as the amount of selected files (5 files selected then array size = 5 (0-4)
        MessageBox.Show(Convert.ToString(string.Format("Size of Array ={0}", XMlTable.Length)));

        for (int test1 = 0; test1 < XMlTable.Length; test1++)
        {
            MessageBox.Show(Convert.ToString(string.Format("Tables used = {0}",XMlTable[test1].Tables.Count)));
            MessageBox.Show(XMlTable[test1].Tables[0].TableName);
        }
}