2

I need to deserialize a bunch of XML files in a .NET 4.0 project that's just been upgraded from .NET 2.0. Because the Serialization functions have changed, deserialization now fails because a bunch of properties in a third party object library my classes are inheriting from are not marked with [XmlIgnore].

Question: Can I call the Deserialize() function from the 2.0 version of System.Xml.dll from a .NET 4.0 project using reflection?

I've tried:

Assembly.Load("System.Xml, Version=2.0.0.0, PublicKeyToken=b77a5c561934e089");

but this failed with FileNotFoundException (it DOES search the GAC, right?)

Community
  • 1
  • 1
Nilzor
  • 18,082
  • 22
  • 100
  • 167

2 Answers2

1

You cannot - AFAIK.

When you set the target framework for your project, that is it. You get either System.Xml 2.0 or 4.0.

Yet, I am confused on the difference between serialisation between 2.0 and 4.0. Surely [XmlIgnore] was there in 2.0?

Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Yes, but in .NET 4.0 deserialization throws an exception if a property without a set method isn't marked with [XmlIgnore] – Nilzor Mar 21 '12 at 09:48
  • Because they're in a third party library which my classes inherit from. – Nilzor Mar 21 '12 at 10:00
1

You could write a wee dll targeted at framework2 to return a v2 version of the class and then populate the v4 version from that. Have a flag in there somewhere / somehoe and you could flip them over as they are loaded.

Are you sure about why deserialisation is failing, I'd have expected it to backwards compatible, in fact the idea that it isn't is quite scary...

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • Yes, I'm sure, and I agree it's scary. This code https://gist.github.com/2145953 will pass in 2.0 but not in 4.0 unless you mark the SomeProperty with [XmlIgnore] . The error you get is InvalidOperationException - error CS0200: Property or indexer (...) cannot be assigned to -- it is read only – Nilzor Mar 21 '12 at 10:12
  • I will try your second assembly-approach though... Worth a shot – Nilzor Mar 21 '12 at 10:14
  • 1
    Here's another reference to the Serialization versioning issue, although It's a bit confusing: http://social.msdn.microsoft.com/forums/en-US/asmxandxml/thread/3dffcfdc-e857-4f01-87f2-67cf02099735 . Says it will be fixed in 3.5 SP1, but it's obviously still there in 4.0 – Nilzor Mar 21 '12 at 10:18