简体   繁体   中英

C# XmlSerialization failing on Windows XP

I have a program that serializes its settings to XML using the XmlSerializer class. It works fine on all Windows 7 machines, but fails on Windows XP SP3 machines with the following error message:

System.InvalidOperationException: Unable to generate a temporary class (result=1).
error CS0012: The type 'System.Windows.UIElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
error CS0012: The type 'System.Windows.IInputElement' is defined in an assembly that is not referenced. You must add a reference to assembly 'PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
error CS0012: The type 'System.Windows.Markup.IQueryAmbient' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

   at System.Xml.Serialization.Compiler.Compile(Assembly parent, String ns, XmlSerializerCompilerParameters xmlParameters, Evidence evidence)
   at System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence, XmlSerializerCompilerParameters parameters, Assembly assembly, Hashtable assemblies)
   at System.Xml.Serialization.XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping, Type type, String defaultNamespace)
   at System.Xml.Serialization.XmlSerializer..ctor(Type type, String defaultNamespace)

PresentationCore and System.Xaml are present in the reference list of the project, and after searching an forums I set Generate serialization assembly from Auto to On, but with no result. Any idea what could go wrong on Windows XP?

PS: I use .net 4, and the problem appears on Windows XP SP3 machines with .net framework 4 (v4.0.30319) and the latest updates.

This could be related to a bug Microsoft won't fix. See this SO post for more information:

SO: XmlSerializer is throwing InvalidOperationException when using the generic type constraint where

Unfortunately the link to the original Microsoft post is down, so I can't say for sure if this applies to your case.

You should be able to fix it by building and deploying the serialization assembly (*.XmlSerializers.dll).

This is just a workaround really but completely avoids dynamically compiling the serializers when they are first used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM