0

I try to use linq to xml to read very big xml file (407 MB):

XDocument xdoc = XDocument.Load(adress);

(adress - is a path to file) But I get out of memory exception due to the fact (as I suppose) that collection becomes more than to 2GB. Xml file looks like that:

<lemmata>
<lemma id="4694" rev="4694"><l t="аддукторен"><g v="ADJS"/></l><f t="аддукторен"><g v="masc"/><g v="sing"/></f><f t="аддукторна"><g v="femn"/><g v="sing"/></f><f t="аддукторно"><g v="neut"/><g v="sing"/></f><f t="аддукторны"><g v="plur"/></f></lemma>
    <lemma id="4695" rev="4695"><l t="аддукторнее"><g v="COMP"/></l><f t="аддукторнее"></f><f t="аддукторней"><g v="V-ej"/></f><f t="поаддукторнее"><g v="Cmp2"/></f><f t="поаддукторней"><g v="Cmp2"/><g v="V-ej"/></f></lemma>
    <lemma id="4696" rev="4696"><l t="аддукция"><g v="NOUN"/><g v="inan"/><g v="femn"/></l><f t="аддукция"><g v="sing"/><g v="nomn"/></f><f t="аддукции"><g v="sing"/><g v="gent"/></f><f t="аддукции"><g v="sing"/><g v="datv"/></f><f t="аддукцию"><g v="sing"/><g v="accs"/></f><f t="аддукцией"><g v="sing"/><g v="ablt"/></f><f t="аддукциею"><g v="sing"/><g v="ablt"/><g v="V-ey"/></f><f t="аддукции"><g v="sing"/><g v="loct"/></f><f t="аддукции"><g v="plur"/><g v="nomn"/></f><f t="аддукций"><g v="plur"/><g v="gent"/></f><f t="аддукциям"><g v="plur"/><g v="datv"/></f><f t="аддукции"><g v="plur"/><g v="accs"/></f><f t="аддукциями"><g v="plur"/><g v="ablt"/></f><f t="аддукциях"><g v="plur"/><g v="loct"/></f></lemma>
<lemma>.....</lemma>
</lemmata>

So, there are many elements. Do I have an opportunity to read just a part of lemma elements in order collection of elements don't exceed the maximum size? Or there are other ways to read this file? Thank you

Alexey Rumin
  • 193
  • 1
  • 6
  • 17
  • You can use a combination of `XmlReader` and LINQ to XML to stream the file. This gives you the benefit of not having to load the entire document into a DOM without forcing you to deal with the fiddly low-level `XmlReader` API. Take a look at [this related answer](http://stackoverflow.com/a/2441694/1320845). – Charles Mager Jul 20 '16 at 13:35
  • Possible duplicate of [Reading Xml with XmlReader in C#](http://stackoverflow.com/questions/2441673/reading-xml-with-xmlreader-in-c-sharp) – Charles Mager Jul 20 '16 at 13:37
  • @Charles Mager Thank you. I will try XmlReader. – Alexey Rumin Jul 20 '16 at 13:41

1 Answers1

2

Have a look at the XmlReader class. It allows you to forward read through the elements pretty quickly so that you can find the one you want, without pulling the whole file into memory.

Simon Williams
  • 1,016
  • 3
  • 11
  • 27
  • thank you! I will try it. But I tested code with small xml document and wrote a lot of code. As I understood here http://stackoverflow.com/questions/14186256/net-out-of-memory-exception-used-1-3gb-but-have-16gb-installed the problem is that my program runs as 32bit process (but it configured for any CPUand I use Windows 10 64 bit) - so it can use only 2GB of memory. – Alexey Rumin Jul 20 '16 at 13:44