Reputation: 141
I am trying to serialize a DataSet
to an XML file.
As can be seen from the sample output all of my tables have names and the dataset is named as well. The dataset namespace is "", changing this had no effect.
The exception being caught is is:
Cannot serialize the DataTable. DataTable name is not set.
As you can see below quite clearly, all the tables have been named.
DataSet Name: TestDataSet
Table: TABLE1
Table: TABLE2
Table: TABLE3
Table: TABLE4
Table: TABLE5
Cannot serialize the DataTable. DataTable name is not set.
Here is the code I am using to serialize the dataset:
using (FileStream stream = new FileStream("db.xml", FileMode.Create))
{
dataSet.WriteXml(stream, XmlWriteMode.WriteSchema);
stream.Close();
}
if the XmlWriteMode
is WriteSchema
the xml file is created and is populated with the schema, the exception is still thrown. If the XmlWriteMode
is IgnoreSchema
and empty file is created and the exception is thrown.
What is causing this problem? I have trawled through loads of postings here and elsewhere on the web and everyone says "set the table name". I have done this but it makes no difference.
Upvotes: 0
Views: 1231
Reputation: 141
This is the solution I have employed and it works. Probably not the most efficient but the XML is now serialized correctly.
private void FixTables(DataTable table)
{
foreach (DataRow row in table.Rows)
{
foreach (DataColumn column in table.Columns)
{
var columnName = column.ColumnName;
if (column.DataType == typeof(DataTable) && row[columnName] != DBNull.Value)
{
DataTable innerTable = (DataTable)row[columnName];
innerTable.TableName = columnName;
}
}
}
}
Upvotes: 0
Reputation: 3241
Based on the comments, the problem is due to some tables in the Dataset not given a Name. it is probably secondary tables (table elements inside the Table1, Table2 etc).
The Solution could be to Iterate through all objects in the Dataset and, if type is DataTable set a generic name.
Upvotes: 1