0

Trying to deserialize an instance of this class

[Serializable]
public class InstalledMeter : ISerializable 
{
    public string Description { get; set; }

    public double Latitude { get; set; }
    public double Longitude { get; set; }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Description", Description);
        info.AddValue("Latitude", Latitude);
        info.AddValue("Longitude", Longitude);
    }
}

The instance:

new InstalledMeter {Description = "Something", Latitude = 0.0, Longitude = 0.0};

My Serialization methods

public static class SerializeString
{
    public static string SerializeObject<T>(this T toSerialize)
    {
        var xmlSerializer = new XmlSerializer(toSerialize.GetType());
        var textWriter = new StringWriter();

        xmlSerializer.Serialize(textWriter, toSerialize);
        return textWriter.ToString();
    }

    public static T XmlDeserializeFromString<T>(this string objectData)
    {
        return (T)XmlDeserializeFromString(objectData, typeof(T));
    }

    public static object XmlDeserializeFromString(string objectData, Type type)
    {
        var serializer = new XmlSerializer(type);
        object result;

        using (TextReader reader = new StringReader(objectData))
        {
            result = serializer.Deserialize(reader);
        }

        return result;
    }
}

But when I serialize the object it has these values enter image description here

Mech0z
  • 3,627
  • 6
  • 49
  • 85
  • Are you having a Culture issue here? It seems you provide the double values with dot as decimal separator, and the result shows comma as decimal separator. – Bazzz Nov 06 '12 at 08:53
  • You aren't showing the xml that you are deserializing from. The value of longitude that is being displayed is the Epsilon value of double. Do double.Epsilon on the watch window and see what you get. Post your xml source and we'll probably be able to figure out precisely what is in your source that's causing this. . – Hardrada Nov 06 '12 at 08:58
  • If I change the types to int it works, but I dont have an XML source, I just serialize InstalledMeter {Description = "Something", Latitude = 0.0, Longitude = 0.0}; to a string with SerializeObject, parse it as a string and deserialize it and yes Epsilon was = 4,94065645841247E-324 – Mech0z Nov 06 '12 at 09:06
  • If I change the properties in InstalledMeter to strings, and then just do .ToString() and Convert.ToDouble() then it works fine, but thats just very ugly code... – Mech0z Nov 06 '12 at 09:21
  • In your code sample, what's the value of the string `installedMeter`? – nick_w Nov 06 '12 at 09:21
  • Xml http://pastebin.com/gGSgcvQn – Mech0z Nov 06 '12 at 09:33

1 Answers1

1

(guessing an answer based on your previous question - Error passing parameters to mvvmcross viewmodels)

If you are doing this in MvvmCross, then I would avoid this xml problem altogether and just use JSON serialization

  var jsonText = JSONConvert.SerializeObject(installedMeter);

and

  var installedMeter = JSONConvert.DeserializeObject<InstalledMeter>(jsonText);

using just:

    public class InstalledMeter
    {
        public string Description { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
    }
Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • 1
    Ended up using IoC, creating a SharedVariables class with an interface on top and then just making the different ViewModels that need it Consumers of that interface, like that better than parsing around some variable between viewmodels – Mech0z Nov 06 '12 at 22:37
  • Good solution - more maintenable, reusable and extensible. – Stuart Nov 07 '12 at 07:23