views:

251

answers:

3

I have a struct called coordinate which is contained in a list in another class called segment.

public struct Coordinate
{
    public double Latitude { get; set; }
    public double Longtitude { get; set; }
    public double Altitude { get; set; }
    public DateTime Time { get; set; }
}

public class Segment
{
    private List<Coordinate> coordinates;
    ...
}

I'd like to serialize the Segment class using the XmlSerializer using Silverlight (on Windows Phone 7). I understand from link text that XmlSerializer doesn't support List<T>. What is the advised way of serializing a resizable array coordinates?

Thanks, Jurgen

+1  A: 

I am of the opinion that one should always design his own serialization formats. It seems to be a minority opinion these days.

You never know what serializer becomes a file format and you really don't want the ability to read your file formats dependent on .NET framework.

Joshua
I agree that you don't want to be framework dependent (so avoid things like `BinaryFormatter` if it existed on this platform), but there are plenty of platform-independent pre-rolled serialization APIs that don't involve re-inventing the wheel.
Marc Gravell
+1  A: 

Have you tried it? Which bit specifically suggests no lists?

For info, as soon as I have all the tools together (VS2010 / Phone 7 sdk etc) I plan on seeing what protobuf-net can do for Phone 7, but XmlSerializer should be a solid default.

Marc Gravell
A bit over halfway through the page"Serialization of ArrayList and Generic ListThe XmlSerializer cannot serialize or deserialize the following:* Arrays of ArrayList* Arrays of List<(Of <(T>)>)"
Jurgen
@Jurgen - it is saying that nested/jagged lists aren't supported. Your question doesn't relate to a nested list. And even that can be worked around simply by adding an extra object in the middle (i.e. you have an array of some object, which *encapsulates* a list of something; which is fine). For info, this is a common limitation; protobuf-net *also* insists on this (with a few exceptions where it will work)
Marc Gravell
@Marc - hmm, yes I missed this. I wonder why the serialization is not working on the Win7 Phone emulator. Will look into this.
Jurgen
@Jurgen - look at the inner-exceptions; `XmlSerializer` is usually very chatty about problems. Have you initialized `coordinates`, for example?
Marc Gravell
@Marc Don't know if this is related but I noticed a whole bunch of System.FormatException messages in the output window when I call the Deserialization. I reinstalled the Win7 phone tools and now serialization works fine. Thanks for the pointers!
Jurgen
+2  A: 

I always prefer the DataContractSerializer over the XmlSerializer. The XmlSerializer is not available in vanilla Silverlight, but the DataContractSerializer is.

It would not have any problem serializing your structures that you showed.

Brian Genisio