Reputation: 7325
I have an object in c# that needs to be saved as file and reused.
So basically what I am doing now is I am serializing a class to xml and I am saving it as a file. The file is aproximatelly 100MB.
Now the problem I am experiencing is when I want to deserialize file to class, I and up with OutOfMemoryException.
I am using the following code:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(file);
Deserialize<T>(xmlDocument.InnerXml);
public static T Deserialize<T>(string xmlContent)
{
var inStream = new StringReader(xmlContent);
var ser = new XmlSerializer(typeof(T));
return (T)ser.Deserialize(inStream);
}
Upvotes: 1
Views: 2063
Reputation: 2192
Here's what my comment would look like in code:
public static T Deserialize<T>(string Filepath)
{
using (FileStream FStream = new FileStream(Filepath, FileMode.Open))
{
var Deserializer = new XmlSerializer(typeof(T));
return (T)Deserializer.Deserialize(FStream);
}
}
Upvotes: 2