读/用C#编写大型XML文件 [英] Read/Write Large XML Files in C#
问题描述
我正在开发使用XML数据库的应用程序。我有,我有读取和写入数据的大型XML文件。 问题是,我不想通过,因为性能问题的整个文件加载在内存中的整个XML文件还不想循环。因为如果我在内存中加载整个文件,这将影响应用程序的性能,并可能会崩溃,因为内存韭菜的应用程序。
I am developing an application with XML database. I have large XML files in which I have to read and write data. The problem is I do not want to load the whole XML file in memory also do not want to loop through the whole file because of performance issue. Because if I load the whole file in the memory this will effect the application performance and may crash the application because of memory leek.
我需要有足够的方式写入和读取XML转换成文件,该文件不会对性能和内存的影响。
I need a sufficient way to write and read the XML into the file which does not effect on performance and memory.
任何帮助将AP preciated。
Any help will be appreciated.
推荐答案
如果这个XML决定是不是你的,你必须处理它(参见MSDN全样本的 http://msdn.microsoft.com/en-us/library/bb387013.aspx )
If this XML decision is not yours and you have to deal with it (see whole MSDN sample http://msdn.microsoft.com/en-us/library/bb387013.aspx)
static IEnumerable<XElement> StreamCustomerItem(string uri)
{
using (XmlReader reader = XmlReader.Create(uri))
{
XElement name = null;
XElement item = null;
reader.MoveToContent();
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
name = XElement.ReadFrom(reader) as XElement;
break;
}
}
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
item = XElement.ReadFrom(reader) as XElement;
if (item != null)
{
XElement tempRoot = new XElement("Root", new XElement(name));
tempRoot.Add(item);
yield return item;
}
}
}
}
}
}
}
但是,如果你控制了决定,请,您应该忘记的XML。有几个选项,这将有助于你和你的应用程序能正常工作,但无太多的麻烦。
BUT, if you control the decision, PLEASE, you should forget about the XML. There are several options that will help you and your application to work properly without to much hassle.
- SQL精简。尼斯和简单的SQL的方式从微软,并且不requiere SQL Server实例。 http://www.microsoft.com/en-us /sqlserver/editions/2012-editions/compact.aspx
- SQL精简版。工程与.NET,甚至Windows 8的应用程序,方便和pretty的稳定。 http://system.data.sqlite.org/index的.html / DOC /主干/网络/ index.wiki
- SQL Compact. Nice and easy SQL approach from Microsoft, and does not requiere SQL server instance. http://www.microsoft.com/en-us/sqlserver/editions/2012-editions/compact.aspx
- SQL Lite. Works with .net and even Windows 8 applications, easy and pretty stable. http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
您甚至可以使用MySQL,MariaDB或者类似的话!
You could even use MySQL, MariaDB or anything similar!
这篇关于读/用C#编写大型XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!