Reading large XML documents in .net

前端 未结 3 548
清歌不尽
清歌不尽 2020-11-30 09:07

I need to read large xml using .net files which can easily be several GB of size.

I tried to use XDocument, but it just throws an System.OutOfMemoryException when I

相关标签:
3条回答
  • 2020-11-30 09:50

    You could try using an XmlTextReader instance.

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

    0 讨论(0)
  • 2020-11-30 09:54

    You basically have to use the "pull" model here - XmlReader and friends. That will allow you to stream the document rather than loading it all into memory in one go.

    Note that if you know that you're at the start of a "small enough" element, you can create an XElement from an XmlReader, deal with that using the glory of LINQ to XML, and then move onto the next element.

    0 讨论(0)
  • 2020-11-30 09:59

    The following page makes an interesting read, providing a means to mine data from XML file without loading it in memory. It allows you to combine the speed of XmlReader with the flexibility of Linq:

    http://msdn.microsoft.com/en-us/library/bb387035.aspx

    And quite an interesting article based on this technique:

    http://blogs.msdn.com/b/xmlteam/archive/2007/03/24/streaming-with-linq-to-xml-part-2.aspx

    0 讨论(0)
提交回复
热议问题