How to make XML deserialization faster?

不羁岁月 提交于 2021-01-27 05:38:16

问题


I have the following piece of code

public static object XmlDeserialize(string xml, Type objType)
{
    StringReader stream = null;
    XmlTextReader reader = null;
    try
    {
        XmlSerializer serializer = new XmlSerializer(objType);
        stream = new StringReader(xml); // Read xml data
        reader = new XmlTextReader(stream);  // Create reader
        return serializer.Deserialize(reader);
    }
    finally
    {
        if(stream != null) stream.Close();
        if(reader != null) reader.Close();
    }
}

The object itself has been generated via xsd.exe and looks kind of like this:

/// <remarks/>
[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;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("StartAge", IsNullable=false)]
    public DemographicsCriteriaStateStartAge[] StartAges {
        get {
            return this.startAgesField;
        }
        set {
            this.startAgesField = value;
        }
    }
    ...

The method is typically called like this:

var obj = (MyObject) XmlDeserialize(someXmlString, typeof(MyObject));

The following line of code always take a pretty large chunk of time (compared to everything else):

XmlSerializer serializer = new XmlSerializer(objType);

What is going on here, e.g. is it compiling a deserialization assembly in the background? Why the performance issue?

What can I do to ameliorate this performance problem?


回答1:


Yes, it is dynamically generating a serialisation assembly at run time. You can change this behaviour in Visual Studio. Go to the project properties and the build section. There is a setting for "Generate serialization assemblies" set it to true. This will generate a file like YourProject.XmlSerialiser.dll when you compile and will stop this bottleneck at run time.

One exception to note, however, is that this setting applies only to proxy types (for example, web service proxies and the like). To actually force Visual Studio 2010 to generate serialization assemblies for regular types, one must either mess with the project file (.csproj) and remove /proxytypes from the Sgen call or generate a post-build step to manually call sgen.exe on the assembly.




回答2:


Try caching the instance of the XmlSerializer for each type at the class level so you don't have to recreate it each time if the same type is used:

class Foo
{
    private static Dictionary<Type, XmlSerializer> xmls = new Dictionary<Type, XmlSerializer>();

    // ...

    public static object XmlDeserialize(string xml, Type objType)
    {
        StringReader stream = null;
        XmlTextReader reader = null;
        try
        {
            XmlSerializer serializer;
            if(xmls.Contains(objType)) {
                serializer = xmls[objType];
            }
            else {
                serializer = new XmlSerializer(objType);
                xmls[objType] = serializer;
            }           

            stream = new StringReader(xml); // Read xml data
            reader = new XmlTextReader(stream);  // Create reader
            return serializer.Deserialize(reader);
        }
        finally
        {
            if(stream != null) stream.Close();
            if(reader != null) reader.Close();
        }
    }
}


来源:https://stackoverflow.com/questions/8145176/how-to-make-xml-deserialization-faster

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!