We have a few XML files that are being read by our applications. The XML format is fixed, and thus we can read them very easily with XmlSerializer.
I use this code to read the XML files and convert them to classes:
public static T FromXml<T>(this string xml) where T : class
{
if (string.IsNullOrEmpty(xml))
{
return default(T);
}
XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
XmlTextReader textReader = new XmlTextReader(new StringReader(xml));
textReader.Normalization = false;
XmlReaderSettings settings = new XmlReaderSettings();
T value;
using (XmlReader reader = XmlReader.Create(textReader, settings))
{
value = (T)xmlserializer.Deserialize(reader);
}
return value;
}
However, there are some performance issues. When calling this code for the first time a specific type for T
is used, the XmlSerializer
generates a Project.XmlSerializer.dll
file.
This is fine, but costs some precious milliseconds (about 900ms in my case). This can be circumvented by generating that assembly on forehand, using the XML Serializer Generator (sgen). This brings down the time to about half. Primarily due to the reading and reflection of the assembly.
I want to optimize this further, by bringing the XmlSerializer
classes inside the assembly the actual classes are in, but I can't find a way to let XmlSerializer
know not to read an external assembly, but use the serializer from the current assembly.
Any thoughts how to do this or an alternative way to make this work? (I can't pre-load them since most of the serialized classes are used at start-up)
The analysis using ANTS Profiler (metrics from other machine, but same pattern):
Plain. Most of the time (300ms + 400ms = 700ms) is lost in generating and loading the XmlSerializer assembly.
With sgen generated assembly. Most of the time (336ms) is lost in loading the XmlSerializer assembly.
When including the actual source of the assembly inside the project, and calling the serializer directly, the action goes down to 456ms (was 1s in first, 556ms in second).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…