The below method reads the value of a string variable, containing XML formatted data, and allocates it to a dataset. This function has worked fine for a couple years, however the underlying data being retrieved has grown recently. I now intermittently see "out of memory exceptions" thrown on the following line:
"StringReader sr = new StringReader(Regex.Replace(XMLText, pattern, "$1"));"
I've seen other solutions, such as the below two links, where people advise to use File.ReadLines or use a StreamReader. However I'm not sure how to fit that in with the dataSet.ReadXml function.
Read Big TXT File, Out of Memory Exception
DataSet ds = new DataSet();
string pattern = @"(</?)(\w+:)";
//[XMLText] is a string variable containing XML downloaded from a WebServices API.
StringReader sr = new StringReader(Regex.Replace(XMLText, pattern, "$1"));
ds.ReadXml(sr);
return ds;
I can only resolve it by closing and reopening the application. Is there a way to optimize this code to prevent this exception? Many thanks in advance.