Using DataTable.WriteXML After Row Deletion



I am attempting to write an XML file from a datatable using DataTable.WriteXML(stream). WriteXML works perfectly fine after I have some insert/update functions that adds or updates the datatable. However, I am trying to delete from the datatable afterwards and then use WriteXML again. It continues writing XML after the closing DocumentElement. Oddly enough, I appear to successfully delete from the actual datatable. The delete (partial code) is as follows:



DataRow[] rows = table.Select(whereString);

for (int i = rows.Count()-1; i >= 0; i--)
{
rows[i].Delete();
}
table.AcceptChanges();


Iterating all the rows in the table and using Console.WriteLine shows that I have the correct data in the datatable after deletion. However, when I use DataTable.WriteXML(), I get an output where the DocumentElement is closed but the XML file continues:


(partial XML output)



<ipaddress />
</XmlData>
</DocumentElement>name>test1</name>
<hostname>test2</hostname>


(continued...)


I would expect the output .XML file to stop after the closing DocumentElement tag but it doesn't.


The code for printing is not unusual:



string filename = "test.xml";
using (Stream stream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write))
{
myDataTable.WriteXml(stream);
}


Does anyone know what I did wrong here? Using Remove() on the datarow results in the same thing.


No comments:

Post a Comment