DataTable/XmlTextReader memory leak

Hi,
I'm using a XmlTextReader to read xml nodes from a file that looks like this:
<Node attr1="attr01" attr2="attr02"><![CDATA[cdata01]]></Node>
<Node attr1="attr11" attr2="attr12"><![CDATA[cdata11]]></Node>
<Node attr1="attr21" attr2="attr22"><![CDATA[cdata21]]></Node>
....
I don't have a root element!
I have a DataTable with 4 columns - ID (int primary-key), A1, A2, D (strings).
I use this method to fill the DataTable:
==============================================================
public void FillDataTable(DataTable myDataTable, string fileName)
{
int count = 0;
//***
FileStream fl = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None);
//Create the reader.
XmlTextReader reader = new XmlTextReader(fl, XmlNodeType.Element, context);
// Load the reader with the data file and ignore all white space nodes.
reader.WhitespaceHandling = WhitespaceHandling.None;
object[] x = new object[3];
// Parse the file and display each of the nodes.
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element:
//use reader.GetAttribute("..."); to get the attributes
x[0] = rowCount++;
x[1] = reader.GetAttribute(0); //line *
x[2] = reader.GetAttribute(1); //line **
break;
case XmlNodeType.CDATA:
//use reader.Value to get the text;
x[3] = reader.Value;
break;
case XmlNodeType.EndElement:
// go to next element
myDataTable.Rows.Add(x);
break;
}
}
reader.Close();
fl.Close();
}
==============================================================
After I do something with the table (i.e. show) I delete the data in table using myDataTable.Rows.Clear(). Then, I load another file in the table.
Unfortunatelly there is a big memory leak somewhere. I used .NET Memory Profiler and I've discovered that the strings taken from xml in lines * and ** are not garbage collected. It could be that these strings remain referenced.
If I replace the lines * and ** with
x[1] = s1; //line *
x[2] = s2; //line **
where s1 and s2 are 2 string variable declared in *** like this:
string s1="attr1", s2="attr2";
everything is fine.
If someone has any idea please share it.
Thanks,
Dumi.
[2559 byte] By [dumiban] at [2007-12-16]

.NET Development

Site Classified