Hi,
My problem: Merging two datasets deletes parent elements, preserves
all children.
I've created two dataSets. Each use the same schema, parent-child
nested tables.
The first dataSet is loaded with historical data read from an XML
file. The second dataSet has current data filled from a dataGrid.
The system takes selected dataGrid data, dataSet updates a child
element value, merges with the first dataSet, and finally writes out
the latest version of the history file.
Inside the file, there is a mix of parent table elements and children.
All the children are preserved and correct, but are assigned to a
subset of parent elements. Most often only two parent elements, the
first two, are created. The children of the first and second parent
elements are properly assigned. But the rest of the children are
assigned to either parent element in a repeatable pattern depending on
which row of the dataGrid is selected. Using AcceptChanges creates a
single parent element that holds all the children.
Thanks in advance.
My environment: .NET, framework 1.1, C#, windows forms, VS 2003.
// Create dataSet to store dataGrid data
dsNew = null;
dsNew = new DataSet();
dsNew.ReadXmlSchema("xmlFile.xsd");
// Create dataSet to store history file
dsHistory = null;
dsHistory = new DataSet();
dsHistory.ReadXmlSchema("xmlFile.xsd");
dsHistory.ReadXml("xmlFile.xml");
// Populate Parent table from dataGrid dataSet
DataTable dayNew = dsNew.Tables["Day"];
foreach(DataRow copyRow in dayRows)
dayNew.ImportRow(copyRow);
// Populate Child table from dataGrid dataSet
DataTable reqNew = dsNew.Tables["Request"];
foreach(DataRow copyRow in reqRows)
reqNew.ImportRow(copyRow);
//
DataRow daRow;
// Loop through update rows based on
// number of children rows selected in datagrid
for(int i = rowCount - 1; this.threshold - 1 < i; i--)
{
// assign the row
daRow = dsNew.Tables["Request"].Rows[i];
// Set the "Accept" column false
daRow[2] = false;
}
// Merge the historical and new dataGrid dataSet
dsNew.Merge(this.dsHistory);
dsNew.WriteXml("xmlFile.xml", XmlWriteMode.IgnoreSchema);
dsNew.Clear();
dsNew.Dispose();
dsHistory.Clear();
dsHistory.Dispose();