简体   繁体   English

反序列化XML文件时C#中的OutOfMemoryException

[英]OutOfMemoryException in c# when deserializing XML file

I have an object in c# that needs to be saved as file and reused. 我在C#中有一个对象,需要保存为文件并重新使用。

So basically what I am doing now is I am serializing a class to xml and I am saving it as a file. 所以基本上我现在要做的是将一个类序列化为xml并将其保存为文件。 The file is aproximatelly 100MB. 该文件大约为100MB。

Now the problem I am experiencing is when I want to deserialize file to class, I and up with OutOfMemoryException. 现在,我遇到的问题是当我想对文件反序列化到类时,我会遇到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);
 }

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);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM