本文介绍了读取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("----------------------------------------------------------------------------------");
                   }
               }
           }

推荐答案

<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文件时抛出了类型'system.outofmemoryexception'的异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 22:42