I need to do some processing on fairly large XML files ( large here being potentially upwards of a gigabyte ) in C# including performing some complex xpath queries. The problem I have is that the standard way I would normally do this through the System.XML libraries likes to load the whole file into memory before it does anything with it, which can cause memory problems with files of this size.
I don't need to be updating the files at all just reading them and querying the data contained in them. Some of the XPath queries are quite involved and go across several levels of parent-child type relationship - I'm not sure whether this will affect the ability to use a stream reader rather than loading the data into memory as a block.
One way I can see of making it work is to perform the simple analysis using a stream-based approach and perhaps wrapping the XPath statements into XSLT transformations that I could run across the files afterward, although it seems a little convoluted.
Alternately I know that there are some elements that the XPath queries will not run across, so I guess I could break the document up into a series of smaller fragments based on it's original tree structure, which could perhaps be small enough to process in memory without causing too much havoc.
I've tried to explain my objective here so if I'm barking up totally the wrong tree in terms of general approach I'm sure you folks can set me right...
XPathReader is the answer. It isn't part of the C# runtime, but it is available for download from Microsoft. Here is an MSDN article.
If you construct an XPathReader with an XmlTextReader you get the efficiency of a streaming read with the convenience of XPath expressions.
I haven't used it on gigabyte sized files, but I have used it on files that are tens of megabytes, which is usually enough to slow down DOM based solutions.
Quoting from the below: "The XPathReader provides the ability to perform XPath over XML documents in a streaming manner".
Download from Microsoft
Gigabyte XML files! I don't envy you this task.
Is there any way that the files could be sent in a better way? E.g. Are they being sent over the net to you - if they are then a more efficient format might be better for all concerned. Reading the file into a database isn't a bad idea but it could be very time consuming indeed.
I wouldn't try and do it all in memory by reading the entire file - unless you have a 64bit OS and lots of memory. What if the file becomes 2, 3, 4GB?
One other approach could be to read in the XML file and use SAX to parse the file and write out smaller XML files according to some logical split. You could then process these with XPath. I've used XPath on 20-30MB files and it is very quick. I was originally going to use SAX but thought I would give XPath a go and was surprised how quick it was. I saved a lot of development time and probably only lost 250ms per query. I was using Java for my parsing but I suspect there would be little difference in .NET.
I did read that XML::Twig (A Perl CPAN module) was written explicitly to handle SAX based XPath parsing. Can you use a different language?
This might also help https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-1044772.html
http://msdn.microsoft.com/en-us/library/bb387013.aspx has a relevant example leveraging XStreamingElement.
In order to perform XPath queries with the standard .NET classes the whole document tree needs to be loaded in memory which might not be a good idea if it can take up to a gigabyte. IMHO the XmlReader is a nice class for handling such tasks.
It seems that you already tried using XPathDocument and could not accomodate the parsed xml document in memory.
If this is the case, before starting to split the file (which is ultimately the right decision!) you may try using the Saxon XSLT/XQuery processor. It has a very efficient in-memory representation of a loaded XML document (the "tinytree" model). In addition Saxon SA (the shema-aware version, which isn't free) has some streaming extensions. Read more about this here.
How about just reading the whole thing into a database and then work with the temp database? That might be better because then your queries can be done more efficiently using TSQL.
I think the best solution is to make your own xml parser that can read small chunks not the whole file, or you can split the large file into small files and use dotnet classes with these files.
The problem is you can not parse some of data till the whole data is available so I recommend to use your own parser not dotnet classes
Have you been trying XPathDocument?
This class is optimized for handling XPath queries efficiently.
If you cannot handle your input documents efficiently using XPathDocument you might consider preprocessing and/or splitting up your input documents using an XmlReader.
You've outlined your choices already.
Either you need to abandon the XPath and use XmlTextReader or you need to break the document up into managable chunks on which you can use XPath.
If you choose the latter use XPathDocument its readonly restriction allows better used of memory.
Since in your case the data size can run in Gbs have you considered using ADO.NET with XML as a database. In addition to that the memory footprint would not be huge.
Another approach would be using Linq to XML with using elements like XElementStream. Hope this helps.
Related
I'm currently working on a program that reads writes a XML file. While this is a simple task, i'm concerned about future issues.
My code reads the streamed data from the XML, and checks every element <x> until an element that matches a criteria is founds, this works quite fast, since the file currently has about 100 <x> elements, but when more elements are added this task will be much slower, specially if the element that matches the criteria is the last one in avery large file.
What approach should I take to minimize the impact of this?
I was thinking about spliting files in smaller ones (containing up to 1000 elements each) and reading from various of those files at the same time. Is this a proper approach to this?
I'm coding in C#, in case it's relevant for a language-specific approach.
You should use one of the available XML APIs of .Net. Which one depends on the size of the XML files. In this question there is a discussion between XDocument (Linq-to-Xml) and XmlReader. To summarize: If your file fits in memory, then use XDocument. If not then use XmlReader.
This sounds like a batch process in your case. Maybe this link: https://www.codeproject.com/Articles/1155341/Batch-Processing-Patterns-with-Taskling will help you. I never did this in C#, but in Java, and it's a good way to resolve this kind of tasks. Hope it will help you.
I need to read potentially large (~300mb) XML files, and edit some of the nodes. Basically I need to:
Read the XML from the start
Whenever I find a node called trgt
Add some text to it
What's the best way to approach this in C#? Which XML classes should I use to find and edit the nodes I need to change?
TIA
VTD-XML is the only XML parsing lib that supports a feature called incremental update. It is also memory efficient and performant. But it requires you to download it as a third party lib.
From my experience of transforming some very large (2GB+) xml files (don't ask!) I found xsl transforms to be the quickest - The engines involved are heavily optimised for such tasks, compare to any manual looping etc you might try.
you can use Linq-to-XML. in short, read with XDocument, parse and add data with Linq. This will not be the fastest code, but will probably be the quickest to write.
If you have memory constraints, you will probably have to parse it manually (i.e. load only part of it in memory, process that part, replace it in the file)
If it's a fairly simple operation similar to find-and-replace, you could try treating it as a normal text file instead of an xml document. I imagine that might be faster than all the xml parsing.
I heard that LINQ to XML has some performance issues and some of my friends recommended me not to use it in my app. I couldn't find anything relevant on MSDN and I do not want to rely on "some internet blog". Does anyone know of a official point of view on this issue or some trustworthy source?
Using LINQ to XML will read the entire file into memory.
If you're reading an enormous XML file (hundreds of megabytes), this is a problem.
Instead, you can use a raw XmlReader, which provides a forward-only view of an XML file and will not read the entire file at once.
If you're dealing with normal-sized XML files, LINQ to XML will be fine.
LINQ to XML is several orders of magnitude easier to use than XmlReader.
You should only use XmlReader if you know that you'll be dealing with 200MB XML files, or if you've measured your performance and proved that the XDocument constructor is being too slow.
Check MSDN:Performance (LINQ to XML) and Performance of LINQ to XML by Eric White - Microsoft
Just google linq vs xmlreader you will have it.
The top result, http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html, leads to a conclusion that it's slower compare to xmlreader (of course, since linq2Xml is built on top of xmlreader), but IMHO it is far better than acceptable, as you gain the flexibility and easier to read/code.
what is the best way to work with xml file that represets a tree.
the xml size is 70mb.
Linq to XML is the easiest way to currently work with xml but this will typically load the entrire tree into memory which in your case with a 70mb file may not be ideal.
However there are ways around this as demonstrated in this blog post from Mick Taulty.
The answer depends on what you want to do with the XML. Generally with files that size you wouldn't want to read it all in one go. As such the following page makes an interesting read, providing a means to mine data from the 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:
Link
If you want to read data from a large xml file XmlTextReader is the way to go.
For .NET 3.5 and up, I prefer using LINQ to XML for all my work towards XML files.
LinqToXml is probably a good bet if you wish to query it in memory, but if you find that you are getting problems with how large your memory footprint is you could use an XMLReader
Linq To XML
Slower for larger documents (large memory footprint)
Queryable
XmlTextReader
Fast
Only one line at a time, so no querying
Since you are already using a DOM, an alternative XML parser you could try is a SAX parser. Instead of loading the entire tree into memory, a SAX parser is event-driven and handles nodes, etc. as it encounters them.
Further Reading: http://www.saxproject.org/event.html
In one of the applications we are developing we do lot of XML processing. Currently we use DOM and XPath for most of the processing and we are not much happy with the performance.
At the moment we are considering of moving XML processing logic to LINQ and our initial investigations suggest LINQ performance is much better than DOM.
Before making these changes I would like to know how others feel about this. Is using LINQ a better option? Any disavantages etc...
Thanks,
Shamika
Thank you very much for your answers. I did some performance tests and as expected XmlReader out performed both XmlDocument and LINQ. Please note that this is only for XML reading.
Also if you need the ease of use of LINQ you can implement LINQ XML processing by using some features of the XmlReader and can get much better performance than XmlDocument. Please refer to "rwwilden" comments for more information.
Thanks.
Using DOM (ie. System.Xml.XmlDocument) is likely to be slower, because of the rich navigation support (all those references start to add up), and this overhead will become more significant as the number of nodes increases.
Simpler object models (System.Xml.Linq.XDocument and System.Xml.XPath.XPathDocument) don't have such complex structures, but allow navigation by other means. This might add to CPU overhead but should save memory.
In the end you need to profile (time and space) in your case, and also consider how much real (user perceived) difference it makes.
But, for ultimate performance don't load the whole document into memory at all: use System.Xml.XmlReader and System.Xml.XmlWriter and do everything in a stream. Of course this adds development cost.
.NET has a rich (maybe too rich) set of XML APIs, which is best (or at least, least worst) for you can only be determined by you making the trade-offs which are best for you.
Personally I would avoid XmlDocument and use either XPathDocument (especially to read, and query with XPath) or XDocument (especially to create) where XmlReader/XmlWriter does not give enough of a performance boost to justify.
I'm not sure you would notice a very large performance improvement using LINQ2XML instead of DOM/XPath. For both DOM and LINQ2XML the document that you iterate over, is represented as an in-memory tree.
If performance really is an issue and you have rather large XML documents, you could take a look at the rudimentary XML streaming support that is implemented in the framework (via XStreamingElement). Also check this Microsoft XML team blog entry.
My take on it is that LINQ -> XML is leaps and bounds easier to use than DOM. It's more intuitive to me and much easier to read IMO.