views:

161

answers:

3

Anyone have any idea why the following XML generated by a data contract serializer in C# works just fine in Windows but not under Linux on Mono?

The XML:

<Message i:type="UserMessage" xmlns="http://schemas.datacontract.org/2004/07/NetTunnel"
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;&lt;type&gt;UserMessage&lt;/type&gt;
<nick>Unnamed</nick><services><Service><enabled>true</enabled><port_ranges i:nil="true"/>
<service_name>vent</service_name></Service></services><state>Created</state>
<userid>1</userid></Message>

The error:

Unhandled Exception: System.Runtime.Serialization.SerializationException: Deserializing 
type 'System.Object'. Expecting state 'EndElement'. Encountered state 'Element' with 
name 'enabled' with namespace 'http://schemas.datacontract.org/2004/07/NetTunnel'.

It also gives me an error if there are no services listed (xml tag <services/>). The services variable is of type List<Service>. Is this just a type Mono can't handle? Would another type be more appropriate? Or is it something else entirely?

+2  A: 

Not the answer you want, but: Every behavior on Mono that is different from .Net is for all I know a bug in Mono. Please(!) file it, especially if it is really that small and easy to reproduce. Discussing it here might help just as fast, but the next guy might run into the same problem, needs to research the issue etc..

Just file it, the Mono guys are awesome and will be more helpful. To aid others in their search: I suggest you update your post with the link to the issue.

Regarding your last question: You shouldn't need to change the type just because Mono might need to deserialize it - and List is just fine.

Benjamin Podszun
Will do, thanks for the link.
Nayruden
A: 

Submitted a bug report here: https://bugzilla.novell.com/show_bug.cgi?id=581611

Would be great to know of any alternatives until this is fixed, though.

Nayruden
+1  A: 

From the comments, you don't need WCF and just want to share data. In which case, I would look at protobuf-net. I'm quite biased (since I'm the author), but it is free (with source), so I'm not recommending it from self gain. Simply; it is a fast, portable serialization API modelled after google's "protocol buffers" data format. Very fast to process (typically much faster than xml, for example) and pretty small on the wire.

If you already using data-contracts, you might be able to tweak them simply by adding unique Order values (it uses this as a numeric identifier):

[DataContract]
public class Foo {
    [DataMember(Order = 1)]
    public int Id {get;set;}

    [DataMember(Order = 2)]
    public string Name {get;set;}
}

(or you can use the specific protobuf-net attributes)

The full source is available, and it works on Mono, regular .NET, CF, etc.

Marc Gravell