What is the best way to parse large XML (size of 1GB) in C#?

后端 未结 5 548
死守一世寂寞
死守一世寂寞 2020-12-03 11:13

I have a 1GB XML file and want to parse it. If I use XML Textreader or XMLDocument, the result is very slow and some times it hangs...

相关标签:
5条回答
  • 2020-12-03 11:52

    You'll have to implement custom logic using xmlreader. xmlreader does not load the full XML into memory before using it, which means you can read it from a stream and process it as such.

    0 讨论(0)
  • 2020-12-03 12:06

    XMLTextreader isn't supposed to hang as it's stream based and just works on chunks of the data.

    If it hangs, it may well be that you are doing something wrong when loading the file.

    0 讨论(0)
  • 2020-12-03 12:12

    I would just like to back up everyone who promotes XmlReader with a performance comparison that I found:

    http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html

    0 讨论(0)
  • 2020-12-03 12:13

    I'm not very familiar with this topic, but afaik the XmlReader-classes ought to work fine for your specific problem. They are, after all, optimized for exactly this.

    0 讨论(0)
  • 2020-12-03 12:14

    XmlDocument is not feasible in this scenario as it will attempt to suck that gigabyte into main memory. I'm surprised that you're finding XmlTextReader to be too slow. Have you tried something like the following?

    using (XmlTextReader rdr = new XmlTextReader("MyBigFile.txt"))
    {
         // use rdr to advance through the document.
    }
    
    0 讨论(0)
提交回复
热议问题