0

Currently one my DAL methods uses the ExecuteXmlReader command to read and append to a StringBuilder object and return XML as a string. I realize this isn't the best way to handle very large files (200MB+) as when the ToString() method is called on StringBuilder it throws an out of memory exception.

There are certain constraints I need to adhere by - I cannot return a XMLReader out of the DAL (db connection stays open, etc), this XML will have to be written to file and keeping it's formatting for various reasons, currently using an XMLTextWriter's WriteRaw method for that purpose.

What would be the best data type to return from the DAL to make all this work with large XML files? I explored the options of using a DataSet, Memory Stream and a Byte Array - but each have their shortfalls. I am looking for the best practice and some code samples would be helpful.

4

1 回答 1

0

您可以使用 XDocument 处理您的 xml 文件。您可以使用 linq to xml 来查询您的元素。下面是一个例子:

XDocument document = XDocument.Load("http://someurl.com/data.xml");

var elements = from element in document.Descendants("SomeElement")
               select element;

Console.WriteLine(elements.First().Value);

如果您也因此遇到内存不足异常(我想您会的),那么您应该看看以下帖子:

http://james.newtonking.com/archive/2007/12/11/linq-to-xml-over-large-documents.aspx

关于 XDocument 类的更多信息可以在这里找到:

http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx

和这里

http://www.hookdonlinq.com/Print.aspx?Page=LINQtoXML5MinuteOverview

祝你好运!

于 2012-04-07T07:34:08.800 回答