Istudent Rana

Istudent Rana

  • 1.4k
  • 340
  • 83.4k

Exception of type 'System.OutOfMemoryException' was thrown

Apr 26 2019 7:48 AM

I am trying to read xml files and load the data to data base.
While reading and adding xml file to list of object.
I get 'System.OutOfMemoryException'.
I could have more than 6000000 xml files which I am not sure
my code:
foreach (var xml in xmlFiles)
{
try
{
XmlRootAttribute xRoot = new XmlRootAttribute
{
ElementName = "CASE",
IsNullable = true
};
XmlSerializer xs = new XmlSerializer(typeof(MyTable), xRoot);
MyTable cs = new MyTable();
using (var sr = new StreamReader(xml))
{
cs = (MyTable)xs.Deserialize(sr);
// cases.Add(cs);

LoadtToLocalDB(cs);

}
}
catch (Exception ex)
{
using (var writer = new StreamWriter(errorLog, false))
{
writer.WriteLine(xml);
writer.WriteLine(ex.Message);
writer.WriteLine("----------------------------------------------------------------------------------");
}
}
}

Answers (4)