如何使 XML 反序列化更快? [英] How to make XML deserialization faster?

查看:22
本文介绍了如何使 XML 反序列化更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

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();
    }
}

对象本身是通过 xsd.exe 生成的,看起来像这样:

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?

推荐答案

是的,它在运行时动态生成序列化程序集.您可以在 Visual Studio 中更改此行为.转到项目属性和构建部分.有一个生成序列化程序集"的设置将其设置为 true.这将在您编译时生成一个类似 YourProject.XmlSerialiser.dll 的文件,并将在运行时阻止此瓶颈.

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.

但是,需要注意的一个例外是此设置仅适用于代理类型(例如,Web 服务代理等).要真正强制 Visual Studio 2010 为常规类型生成序列化程序集,必须要么混淆项目文件 (.csproj) 并从 Sgen 调用或生成构建后步骤以在程序集上手动调用 sgen.exe.

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.

这篇关于如何使 XML 反序列化更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆