I have the following code snippet
public static object XmlDeserialize(string xml, Type objType)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlSerializer serializer = new XmlSerializer(objType);
stream = new StringReader(xml);
reader = new XmlTextReader(stream);
return serializer.Deserialize(reader);
}
finally
{
if(stream != null) stream.Close();
if(reader != null) reader.Close();
}
}
The object itself was generated using xsd.exe and looks something like this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class MyObject {
private DemographicsCriteriaStateStartAge[] startAgesField;
private DemographicsCriteriaStateEndAge[] endAgesField;
private DemographicsCriteriaStateFilter[] selectedFiltersField;
[System.Xml.Serialization.XmlArrayItemAttribute("StartAge", IsNullable=false)]
public DemographicsCriteriaStateStartAge[] StartAges {
get {
return this.startAgesField;
}
set {
this.startAgesField = value;
}
}
...
The method is usually called the following:
var obj = (MyObject) XmlDeserialize(someXmlString, typeof(MyObject));
The following line of code always takes a fairly large chunk of time (compared to everything else):
XmlSerializer serializer = new XmlSerializer(objType);
What is going on here, for example. Is it building a deserialization assembly in the background? Why is there a performance problem?
What can I do to improve this performance problem?
source
share