C# XmlSerializer BindingFailure

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:27:19  来源:igfitidea点击:

C# XmlSerializer BindingFailure

c#.netvisual-studio-2008xmlserializer

提问by Steve H.

I get a BindingFailure on a line of code using the XmlSerializer:

我使用 XmlSerializer 在一行代码上得到一个 BindingFailure:

XmlSerializer s = new XmlSerializer(typeof(CustomXMLSerializeObject));

The assembly with display name CustomXMLSerializeObject.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 XMLSerializeObject.XmlSerializers, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

显示名称为 CustomXMLSerializeObject.XmlSerializers' 的程序集无法在 ID 为 1 的 AppDomain 的“LoadFrom”绑定上下文中加载。失败的原因是:System.IO.FileNotFoundException:无法加载文件或程序集 XMLSerializeObject.XmlSerializers,版本=1.4.0.0,Culture=neutral,PublicKeyToken=null' 或其依赖项之一。该系统找不到指定的文件。

The error is quite long and goes on to explain pre-bind state information and the places it looked to try and find the file.

该错误很长,并继续解释预绑定状态信息以及尝试查找文件的位置。

The custom object I am trying to desrialize is relatively simple - just a bunch of private integers and strings that have public accessors. I do have a private variable that is another custom serializeable class but that one has nothing but private strings with public accessors in it.

我试图反序列化的自定义对象相对简单 - 只是一堆具有公共访问器的私有整数和字符串。我确实有一个私有变量,它是另一个自定义的可序列化类,但它只有带有公共访问器的私有字符串。

The awkward part? This only happens when I deserialize. That line of code runs fine when I serialize the object. It works fine and the object gets deserialized and populated perfectly. Don't really notice any loss of performance or long loading time.

尴尬的部分?这仅在我反序列化时发生。当我序列化对象时,这行代码运行良好。它工作正常,对象被反序列化并完美填充。不要真正注意到任何性能损失或加载时间过长。

What exactly is this warning (not an error or exception, program runs fine afterwards)? Why does it happen? How do I prevent it without simply disabling the warning?

这个警告到底是什么(不是错误或异常,程序运行良好)?为什么会发生?如何在不禁用警告的情况下防止它?

采纳答案by SwDevMan81

According to Strange XmlSerializer error:

根据奇怪的 XmlSerializer 错误

This exception is a part of the XmlSerializer's normal operation. It is expected and will be caught and handled inside of the Framework code. Just ignore it and continue. If it bothers you during debugging, set the Visual Studio debugger to only stop on unhandled exceptions instead of all exceptions.

此异常是 XmlSerializer 正常操作的一部分。它是预期的,并将在框架代码中被捕获和处理。只需忽略它并继续。如果它在调试过程中困扰您,请将 Visual Studio 调试器设置为仅在未处理的异常而不是所有异常上停止。

Its probably being caused based on your exceptions that you are choosing to monitor.

它可能是根据您选择监视的异常引起的。

Can you tell me how your exceptions are setup: Debug -> Exceptions

你能告诉我你的异常是如何设置的:调试 -> 异常

If you uncheck the "Thrown" checkbox for the BindingFailure under the Managed Debugging Assistants the exception should go away. Or if you dont want to do this, you can just continue since this exception is by design

如果取消选中 Managed Debugging Assistants 下 BindingFailure 的“Thrown”复选框,异常应该消失。或者,如果您不想这样做,您可以继续,因为此异常是设计使然

回答by Lucas B

According to MS VS 2010 Feedbackthis is how it was designed. In order to prevent this exception and prevent a slow-down during run-time execution you need to generate a XML Serializer assembly.

根据MS VS 2010 反馈,这就是它的设计方式。为了防止此异常并防止在运行时执行期间变慢,您需要生成一个 XML 序列化程序程序集。

There are three tools I could find: Microsoft SGen, XGenPlusand Mvp.Xml.XGen. As of this post, unfortunately, none of these has been updated since 2007.

我可以找到三个工具:Microsoft SGenXGenPlusMvp.Xml.XGen。遗憾的是,截至本文发布时,这些自 2007 年以来均未更新。

回答by ouflak

Alright I've found a solution. I never could accept turning off exceptions as an answer. Just seems somehow wrong....

好的,我找到了解决方案。我永远无法接受关闭异常作为答案。只是好像有点不对劲......

What seems to be happening is that in previous assemblies, or previous versions of your current assembly, certain references were used externally. Even though your code may have long since abandoned those references, the names are still, some mysterious somewhere, being searched for in the assembly.

似乎正在发生的是,在以前的程序集或当前程序集的以前版本中,某些引用是在外部使用的。尽管您的代码可能早就放弃了这些引用,但这些名称仍然在某处神秘地在程序集中被搜索。

Go to your AssemblyInfo.cs files and find ThemeInfo:

转到您的 AssemblyInfo.cs 文件并找到 ThemeInfo:

[assembly: ThemeInfo(
ResourceDictionaryLocation.ExternalAssembly, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

Change the first location to 'None':

将第一个位置更改为“无”:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

And keep your exceptions turned on! I will be posting this answer to various questions of this similar nature.

并保持您的例外开启!我将针对类似性质的各种问题发布此答案。

回答by Lin Song Yang

Use the following method to construct your xmlSerializer instance will fix the problem:

使用以下方法构建您的 xmlSerializer 实例将解决该问题:

XmlSerializer s = XmlSerializer.FromTypes(new[] { typeof(CustomXMLSerializeObject) })[0];

then, you don't need to turn off the exception handlings.

然后,您不需要关闭异常处理。