尝试反序列化此类的实例
[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);
}
}
实例:
new InstalledMeter {Description = "Something", Latitude = 0.0, Longitude = 0.0};
我的序列化方法
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;
}
}
但是当我序列化对象时,它具有这些值
答案 0 :(得分:1)
(根据您之前的问题猜测答案 - Error passing parameters to mvvmcross viewmodels)
如果你在MvvmCross中这样做,那么我会完全避免这个xml问题而只是使用JSON序列化
var jsonText = JSONConvert.SerializeObject(installedMeter);
和
var installedMeter = JSONConvert.DeserializeObject<InstalledMeter>(jsonText);
只使用:
public class InstalledMeter
{
public string Description { get; set; }
public double Latitude { get; set; }
public double Longitude { get; set; }
}