How/Can I use linq to xml to query huge xml files with reasonable memory consumption?

前端 未结 3 1541
孤城傲影
孤城傲影 2020-12-03 11:09

I\'ve not done much with linq to xml, but all the examples I\'ve seen load the entire XML document into memory.

What if the XML file is, say, 8GB, and you really don

3条回答
  •  無奈伤痛
    2020-12-03 11:38

    Using XElement.Load will load the whole file into the memory. Instead, use XmlReader with the XNode.ReadFrom function, where you can selectively load notes found by XmlReader with XElement for further processing, if you need to. MSDN has a very good example doing just that: http://msdn.microsoft.com/en-us/library/system.xml.linq.xnode.readfrom.aspx

    If you just need to search the xml document, XmlReader alone will suffice and will not load the whole document into the memory.

提交回复
热议问题