XmlSerializer.Serialize需要很长时间......为什么?

时间:2011-12-07 00:59:16

标签: .net c#-4.0 xmlserializer sgen

在RssXmlHelper.cs中给出RssToolkit中的以下代码:

    /// <summary>
    /// Returns XML of the Generic Type.
    /// </summary>
    /// <param name="rssDocument">The RSS document.</param>
    /// <typeparam name="T">RssDocumentBase</typeparam>
    /// <returns>string</returns>
    public static string ToRssXml<T>(T rssDocument) where T : RssDocumentBase
    {
        if (rssDocument == null)
        {
            throw new ArgumentNullException("rssDocument");
        }

        using (StringWriter output = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(output, rssDocument);
            return output.ToString();
        }
    }

在我的WCF服务中调用serializer.Serialize()时,需要花费很多时间。

我坐在我从项目中调用的WCF服务中。我踏入它,果然,这就是问题所在。

然后我在我的解决方案中引用该项目,没问题。

在WCF服务中使用它时,我应该做些什么?

- 更新 -

好的,所以我安装了适用于Windows 7和.NET Framework 4的Microsoft Windows SDK,运行了sgne.exe RssToolkit.dll并收到以下错误:

Error:  An attempt was made to load an assembly with in incorrect format [path to rsstoolkit.dll]
- Could not load file or assemply [path to rsstoolkit.dll] or one of its dependencies.  This assembly was build by a runtime newer than the currently loaded runtime and cannot be loaded.

我的RssToolkit项目的目标框架设置为4.0,与.Net 4的Windows SDK相匹配。这不正确吗?

1 个答案:

答案 0 :(得分:3)

您可以使用sgen.exe生成序列化程序集,这是Windows SDK附带的XML Serializer Generator Tool。通过在大班上使用它,我看到了极大的改进。

它基本上会生成知道如何序列化和反序列化域中每个对象的代码。生成DLL后,可以将其作为程序集引用包含在项目中,然后使用其中的类而不是您现在使用的XmlSerializer