3

I stumbled upon a problem with the .NET XmlSerializer. Whenever I try to deserialize some of my projects classes I get an FileNotFound-Exception with the following message:

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.

I know that I get that exception because I disabled the "Just My Code"-Option in the visual studio. I actually want to get that exception so I can make sure I generate an XmlSerializers.dll for the project that it's missing for, but as you can see in this case it's the mscorlib.dll. I can't seem to figure out what the problem is. When I use the sgen.exe and try to generate an XmlSerializers.dll for the project in which the class is that I am trying to deserialize I get some warnings, but the XmlSerializers.dll is generated. The class that is being deserialized has nothing special. It contains some properties of the following types

2x short
1x string
2x DateTime
1x List<short>
1x List<MyClass>

'MyClass' contains nothing special either, just some shorts, a string and one DateTime property.

I have other classes that basically have the same spectrum of datatypes and they deserialize just fine. Is this maybe a bug in the CLR?

I created a testproject and copied the 2 classes and deserialized them and got no exceptions.

My CLR Version is 2.0.

If you need more information on the subject let me know I would be glad to provide whatever you need if it helps me resolving this issue. Any help will be appreciated.

Thanks in advance.

Greetings,

Andre

Update:

Like sll requested here are the Classes

SerialzedClass:

public class SerializedClass
{
#region Constructor
/// <summary>
/// constructor for serialization only
/// </summary>
public SerializedClass() { }

/// <summary>
/// public constructor
/// </summary>
public SerializedClass(short id, string bez, DateTime updateY, DateTime updateX, short lastID)
{

  this.ID = id;
  this.Bez = bez;
  this.UpdateY = updateY;
  this.UpdateX = updateX;
  this.LoadedSprs = new List<short>();
  this.LoadedHandRefs = new List<TraderHandRef>();
  this.LastID = lastID;

} // SerializedClass

#endregion

public short LastID { get; set; }

public short ID { get; set; }

public string Bez { get; set; }

public DateTime UpdateY { get; set; }

public DateTime UpdateX { get; set; }

public List<short> LanguageIDs { get; set; }

public List<MyClass> MyClass { get; set; }

}

MyClass:

public class MyClass
{

#region Constructor

/// <summary>
/// constructor for serialization only
/// </summary>
public MyClass() { }

#endregion

#region Properties

/// <summary>
/// get/set KID value
/// </summary>
public short KID { get; set; }

/// <summary>
/// get/set Date value
/// </summary>
public DateTime Date { get; set; }

/// <summary>
/// get/set VID value
/// </summary>
public short VID { get; set; }

/// <summary>
/// get/set description value
/// </summary>
public string Description { get; set; }

/// <summary>
/// get/set ID value
/// </summary>
public short ID { get; set; }

#endregion
}

My deserialize call looks like this:

System.Xml.Serialization.XmlSerializer serial = new System.Xml.Serialization.XmlSerializer(typeof(T));

where T represents SerializedClass.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Andre Fly
  • 156
  • 9
  • 1
    possible duplicate of [FileNotFoundException for mscorlib.XmlSerializers.DLL, which doesn't exist](http://stackoverflow.com/questions/12135/filenotfoundexception-for-mscorlib-xmlserializers-dll-which-doesnt-exist) – sll Oct 11 '11 at 12:53
  • the answer in that post didn't help my problem, I could've posted my question there though. – Andre Fly Oct 11 '11 at 12:57
  • 1
    ok show code which actually does serialization and deserialization, are you deserializing exactly the same type which was serialized? – sll Oct 11 '11 at 13:01
  • check original post, I added the classes and the serialization call – Andre Fly Oct 11 '11 at 13:16

3 Answers3

0

I believe this is an expected error. C# XmlSerializer BindingFailure describes the problem and why it occurs

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184
  • Like I said on top of my post, I know that this error is expected when a XmlSerializers.dll is not available. The question is why is the mscorlib.XmlSerializers.dll not available at that point or what am I doing wrong that it's not being generated. – Andre Fly Oct 11 '11 at 14:01
0

I think you need to add the Serializable attribute to your class.

[Serializable()]
public class SerializedClass
{
Mark Kram
  • 5,672
  • 7
  • 51
  • 70
  • Hey Johnny, thanks for the down vote! I had the same exact issue last night and I solved it by added the Serialize attribute to my class. – Mark Kram Oct 11 '11 at 13:50
  • 1
    Hey Mark, I have the Serializable attribute, it just looked weird in the post so I removed it, sorry. – Andre Fly Oct 11 '11 at 13:59
  • No worries Andre, I was doing the same thing last night and I received the same (or similar) message. I isolated the class into a separate project and it worked fine but once I added the Serialize attribute it worked like a champ. Good luck! – Mark Kram Oct 11 '11 at 14:42
0

You could try generating the serialization assembly for mscorlib:

REM In a Visual Studio Prompt:
sgen /a:C:\Windows\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll /o:.\x32
sgen /a:C:\Windows\assembly\GAC_64\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll /o:.\x64
Jonathan Dickinson
  • 9,050
  • 1
  • 37
  • 60
  • I am unsure about that Jonathan. Where would I put that generated serialization assembly? The output of my project is a Client application. As far as I know anyone with whatever version of .NET 2.0 could run this program and so some or most of the people would still have this problem. Thanks anyway. – Andre Fly Oct 11 '11 at 14:08
  • @AndreFly it would probably work directly in your client's directory; but you would need to retain two directories for x86 and x64 and wire them in with the probing configuration. http://msdn.microsoft.com/en-us/library/823z9h8w.aspx – Jonathan Dickinson Oct 11 '11 at 14:10
  • @AndreFly I think you a pretty much out of luck then :(. – Jonathan Dickinson Oct 11 '11 at 19:38