DataTable包含一个不存在的列c#

时间:2014-07-24 13:18:14

标签: c# datatable

我一直在尝试重置数据表,以便用户可以加载另一个xml文件。 虽然我不断获得重复的名称异常,即使数据表没有附加任何列。

代码段示例,

string rdr = (Path.ChangeExtension(filepath, ".xsd"));
ds.ReadXmlSchema(rdr);
ds.ReadXml(xmlFile);
resetDataTable(dt);
Console.WriteLine(dt.Columns.Count);//This gives 0
foreach (DataColumn c in ds.Tables[1].Columns)//ds.Tables[1] is the sheet that the data is on, 
//it's probably due to how the xml are laid out. They're created out of a script, so they always follow the 
//same rules
{
    if(dt.Columns.Contains(c.ColumnName)){
        //It enters here even though it has no columns
        dt.Columns.Remove(c.ColumnName);
        //ArgumentException trying to delete a column that doesn't 
        //belong to the datatable
    }
    dt.Columns.Add(c.ColumnName);
    //Will crash here normaly saying the column already exists.
}
private void resetDataTable(DataTable dt){
     dt.Columns.Clear();
     dt.Clear();
}

0 个答案:

没有答案