当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - 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.

网友答案:

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?

网友答案:

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.

网友答案:

You can try implementing IXmlSerializable in your "T" class write custom logic to process the XML.

分享给朋友:
您可能感兴趣的文章:
随机阅读: