Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most efficient way to Deserialze an XML file

Aloha,

I have a 8MB XML file that I wish to deserialize. I'm using this code:

public static T Deserialize<T>(string xml)
{
    TextReader reader = new StringReader(xml);
    Type type = typeof(T);
    XmlSerializer serializer = new XmlSerializer(type);
    T obj = (T)serializer.Deserialize(reader);
    return obj; 
}

This code runs in about a minute, which seems rather slow to me. I've tried to use sgen.exe to precompile the serialization dll, but this didn't change the performance.

What other options do I have to improve performance?

[edit] I need the object that is created by the deserialization to perform (basic) transformations on. The XML is received from an external webservice.

like image 312
edosoft Avatar asked Oct 29 '25 14:10

edosoft


2 Answers

The XmlSerializer uses reflection and is therefore not the best choice if performance is an issue.

You could build up a DOM of your XML document using the XmlDocument or XDocument classes and work with that, or, even faster use an XmlReader. The XmlReader however requires you to write any object mapping - if needed - yourself.

What approach is the best depends stronly on what you want to do with the XML data. Do you simply need to extract certain values or do you have to work and edit the whole document object model?

like image 147
Dirk Vollmar Avatar answered Nov 01 '25 05:11

Dirk Vollmar


Yes it does use reflection, but performance is a gray area. When talking an 8mb file... yes it will be much slower. But if dealing with a small file it will not be.

I would NOT saying reading the file vial XmlReader or XPath would be easier or really any faster. What is easier then telling something to turn your xml to an object or your object to XML...? not much.

Now if you need fine grain control then maybe you need to do it by hand.

Personally the choice is like this. I am willing to give up a bit of speed to save a TON of ugly nasty code.

Like everything else in software development there are trade offs.

like image 33
Derik Whittaker Avatar answered Nov 01 '25 04:11

Derik Whittaker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!