为什么我会得到一个序列化错误? [英] Why am I getting a serialization error?

查看:113
本文介绍了为什么我会得到一个序列化错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

class Program
{
    static void Main(string[] args)
    {
        string xml = @"<ArrayOfUserSetting>
                            <UserSetting>
                                <Value>Proposals</Value>
                                <Name>LastGroup</Name>
                            </UserSetting>
                            <UserSetting>
                                <Value>Visible</Value>
                                <Name>WidgetsVisibility</Name>
                            </UserSetting>
                        </ArrayOfUserSetting>";

        List<UserSetting> settings = 
                 GetObjFromXmlDocument<List<UserSetting>>(xml);
    }

    public static T GetObjFromXmlDocument<T>(string xml)
    {
        T customType;

        XmlSerializer serializer = new XmlSerializer(typeof(T));

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.LoadXml(xml);
        using (XmlNodeReader xmlNodeReader = new XmlNodeReader(xmlDocument))
        {
            customType = (T)serializer.Deserialize(xmlNodeReader);
        }

        return customType;
    }
}

[Serializable]
public class UserSetting
{
    public string Value { get; set; }
    public string Name { get; set; }
}

在code正常工作,并调用GetObjFromXmlDocument产生一个List集合。但是,我总是第一个机会异常mscorlib.dll中,键入 System.IO.FileNotFoundException 时, XmlSerializer的序列化=新的XmlSerializer(typeof运算(T )); 执行

于是我走进调试/异常,并开启托管调试助手。我得到了以下在该行:

So I went into Debug/Exception and turned on Managed Debugging Assistants. I got the following on that line:

与显示名称mscorlib.XmlSerializers的组件未能加载在'LoadFrom'结合的AppDomain的上下文ID 1.失败的原因是信息:System.IO.FileNotFoundException:无法加载文件或程序集 mscorlib.XmlSerializers,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089或它的某一个依赖。该系统找不到指定的文件。   文件名:mscorlib.XmlSerializers,版本= 2.0.0.0,文化=中性公钥= b77a5c561934e089

The assembly with display name 'mscorlib.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified. File name: 'mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

有人可以解释为什么发生这种情况?有什么我可以做的 UserSetting 类来使问题消失?该应用程序是相当敏感的表现,我宁愿没有例外。

Can someone explain why this is happening? Is there something I could do to the UserSetting class to make the problem disappear? The application is quite performance sensitive and I'd rather not have the exception.

推荐答案

<一个href="http://web.archive.org/web/20101023030415/http://connect.microsoft.com/VisualStudio/feedback/details/93647/xml-serializer-throws-file-not-found-exceptions"相对=nofollow>微软称:

XmlSerializer的尝试加载   pre生成的序列化,以避免   的序列化code编译   上飞。有没有简单的方法来   检查将装配由被发现   所述的Assembly.Load()调用,这将是   复制融合路径搜索和   在XmlSerializer的加载逻辑。

XmlSerializer attempts to load pre-generated serializers to avoid compilation of the serialization code on the fly. There is no easy way to check for "will assembly be found by the Assembly.Load() call", it would be duplicating Fusion path search and loader logic in XmlSerializer.

它看起来像一个FileNotFound异常被抛出,并在XmlSerializer的处理时,将产生pre-生成序列不能被发现,这将进而导致序列化code。

It looks like a FileNotFound exception is thrown and handled in the XmlSerializer when the "pre-generated serializer" cannot be found, which will then cause the serialization code to be generated.

这篇关于为什么我会得到一个序列化错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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