读取XML文件时抛出了类型'system.outofmemoryexception'的异常。 [英] Exception of type 'system.outofmemoryexception' was thrown while reading XML files.

查看:114
本文介绍了读取XML文件时抛出了类型'system.outofmemoryexception'的异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

if possible please guide better way to optimize the code.





我尝试过:





What I have tried:

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

推荐答案

您的代码尝试加载整个将xml文件存入内存,因此如果文件太大,您将获得该异常。您可能需要更改代码以使用XmlReader,它可以一次读取单个节点



XmlReader.Read方法(System.Xml)| Microsoft Docs [ ^ ]



这意味着完全改变你的代码。



如果您的xml看起来像这样(请注意,重要的是您只有一个根项目)



Your code tries to load the entire xml file into memory, so if the file is too big you will get that exception. You might need to change your code to use XmlReader which can read a single node at a time

XmlReader.Read Method (System.Xml) | Microsoft Docs[^]

It will mean completely changing your code however.

If your xml looks like this (note it is important you only have one root item)

<students>
	<student>
		<name>John Does</name>
		<age>14</age>
		<gender>MALE</gender>
		<class>8</class>
	</student>
	<student>
		<name>John Does 2</name>
		<age>24</age>
		<gender>FEMALE</gender>
		<class>9</class>
	</student>
</students>





然后,此代码基本上将节点的值存储在集合中并处理它们一个人。这是一个控制台应用程序作为示例;





Then this code will basically store the values of your nodes in a collection and process them one by one. It's a console app as an example;

static void Main(string[] args)
{
    XmlReader r = XmlReader.Create(@"c:\temp\test.xml");

    NameValueCollection values = null;
    string nodeName = null;

    while (r.Read())
    {
        if (r.NodeType == XmlNodeType.Element)
        {
            if (r.Name.Equals("student", StringComparison.CurrentCultureIgnoreCase))
            {
                ProcessStudent(values);

                values = new NameValueCollection();
            }
            else if (values != null)
            {
                nodeName = r.Name;
            }
        }
        else if (r.NodeType == XmlNodeType.Text && !string.IsNullOrWhiteSpace(nodeName))
        {
            values.Add(nodeName, r.Value);
        }
    }

    ProcessStudent(values);

    Console.ReadLine();
}

static void ProcessStudent(NameValueCollection values)
{
    if (values == null)
    {
        return;
    }

    foreach (string key in values.Keys)
    {
        Console.WriteLine(key + " = " + values[key]);
    }
}


首先查看异常详细信息,找出哪些文件存在问题(假设是每次都是相同的文件;如果不是那么你需要查看正在加载的文件数量以及其他代码中发生的情况)。



然后手动检查文件以及XML本身是否有任何明显的东西。

如果这一切看起来都没问题,那就捏造你的代码只加载那个文件并通过使用调试器来查找代码在该代码中出现问题。



至少应该告诉你为何得到例外的线索。
Start by looking at the exception detail, and finding out which of your files is giving a problem (assuming it's the same file each time; if it isn't then you need to look at how many files are being loaded and what happens to them in your other code).

Then manually check the file and if there is anything obvious in the XML itself.
If that all looks OK, fudge your code to only load that file and follow your code through using the debugger to find out where in that code a problem occurs.

That at least should give you clues as to why you are getting the exception.


这篇关于读取XML文件时抛出了类型'system.outofmemoryexception'的异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆