Quickest/best way to read XML - c#

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.

Related

Best approach to reading large files

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.

What is the best way to work with xml?

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

How best to use XPath with very large XML files in .NET?

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.

Read/write XML with C#/.NET

What are the best functions, practices, and/or techniques to read/write XML with C#/.NET?
If you are working with .NET 3.5, LINQ to XML is certainly a very good way to interact with XML.
MSDN Link
There are classes to read XML:
XmlDocument is slow and memory-intensive: it parses the XML and loads it into an in-RAM DOM, which is good if you want to edit it.
XmlReader is less memory-intensive: it scans the XML from front to back, never needing to keep all of it in RAM at once.
Similarly, for writing you can construct an XmlDocument and then save it, or use an XmlWriter.
After I wrote the above, there's now a new set of APIs which are easier to use: i.e. for example the XDocument and XElement classes.
By far the simplest method I've found for dealing with XML in C# is to use the XML Serialization tools. For example: http://www.dotnetjohn.com/articles.aspx?articleid=173.
Essentially, you can define C# classes that match your XML file (in fact, you can have them created for you if you have an XML definition file) and then you simply initialize instances of those classes directly from the XML file. Once you have them as instances, you can manipulate them as you wish and rewrite them back into XML files just as easily.
In a performance critical application XmlReader/XmlWriter are a good choice (see here) for the sake of simplicity which is offered by Linq to XML and XmlDocument.
I've found the MvpXml project very useful in past scenarios where performance is a consideration. There's a wealth of knowledge about good practice within their project pages: http://www.codeplex.com/MVPXML

Save attribute value of xml element with single quotes using linq to xml

How do I make the XDocument object save an attribute value of a element with single quotes?
I'm not sure that any of the formatting options for LINQ to XML allow you to specify that. Why do you need to? It's a pretty poor kind of XML handler which is going to care about it...
As long as you use single- and double-quotes in matched pairs and with correct nesting, standards-compliant XML processors won't care which style you use. Your question suggests that you are intending to process your XML output with tools that are not standards-compliant (or perhaps even not XML-aware). This is a dicey proposition at best, though I recognize that work situations and customer demands may not always give you the options of working with the right tools. I have co-workers who use sed and grep to sift through and modify XML files, and they often can get away with that. But if you have any choice at all, I recommend that you handle XML files with XML-aware tools all along the pipeline up to the point where the data is no longer marked up in XML. Doing otherwise will result in systems that are much more fragile than if you used XML-aware tools for all XML processing.
If you can't do that, then JacobE's suggestion is probably your best bet.
If it is absolutely necessary to have single quotes you could write your XML document to a string and then use a string replace to change from single to double quotes.

Categories

Resources