Hi,
the typing problem is caused by the fact that all overloads of 'Deserialize' method return 'obj' type, but your code expects that the return type will be of type 'aSerializableClass' (or the method will be generic). The correct version would be to add 'unbox' or downcast operator (:?>):

1
2
 
let aSC2:aSerializableClass = unbox (xs2.Deserialize(sr))

Anyway, a bad news is that XML serialization unfortunately doesn't work very well on classes produced by F# (mainly because F# compiles fields that you write as properties wrapping the real fields and as far as I remember these are (or at least were in earlier versions) public, which confuses the XML serializer).

You could look at some ways for customizing how the XML serialization works using attributes and it may be possible to workaround these issues, but I have not tried it. Here are some links to MSDN that may help: [link:msdn2.microsoft.com], [link:msdn2.microsoft.com].

BTW: You also don't need the 'Serializable' attribute, unless you want to use binary serialization, which returns byte array, but I think it will work fine with F# classes, so it may be better choice than XML serialization in some cases.

By on 12/1/2007 4:48 AM ()

Hi all,

I believe XML Serialization now works just fine: see [link:cs.hubfs.net]. Please let us know if you spot any outstanding problems.

Thanks!

don

By on 12/1/2007 6:56 AM ()

Hm.. you're probably right.
I confused this with a problem that I had with WSDL tool - this relies on XML Serialization internally, but fails, because the generated XML is different than what you get from C# (but that doesn't mean that serialization & deserialization shouldn't work).

By on 12/1/2007 7:53 AM ()

Thanks for the quick responses. I should have seen the unbox.

The code now works as it should, producing:

<?xml version="1.0" encoding="utf-16"?>
<aSerializableClass xmlns:xsi="[link:www.w3.org]" xmlns:xsd="[link:www.w3.org]">
<_j1>1</_j1>
</aSerializableClass>

// and later . . .

1

A few notes:

  • It is a good point to clarify the usage of the [<Serializable>] attribute in this context.
  • There is a difference between F# and C# serialized class XML schema. If there is a need to interop F#/C# serialization, e.g. deserialize a C# object into an F# object, this should be reviewed. WSDL, SQLCLR and some Code-First approaches would be candidates to consider. IMHO, XML Serialization is not an approach to Contract-First work, e.g. WCF, SQL Server Service Broker, BizTalk, etc.
  • There is NO functional difference (F#/C#) that I can otherwise see and there is NO impact in my scenario w.r.t. XML schema differences.

I started running some tests with a few serialize/deserialize pairs and hope to have another larger test later in the week. So far F# XMLSerialization works as advertised.

It is ironic that two XMLSerializer questions were posted within such a short amount of time.

Thanks again,

---O

By on 12/1/2007 8:13 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper