反序列化XML的IOException

时间:2010-08-15 07:05:01

标签: c# xml-serialization

我正在为Windows Mobile 6.1(.NET CF 3.5)编写一个小型导航应用程序,当我尝试从文件流反序列化我的数据时,我收到了IOException,我无法理解原因。 这是我的一些代码:

//That's the class I am trying to serialize / deserialize
 public class MapData
    {
        [XmlIgnore]
        public Bitmap EntireMapBitmap { get; set; }
        public string Date { get; set; }
        public string FileName { get; set; }
        public Route NavigationRoute { get; set; }
       //and some other unrelavant fields...
    }

这是序列化的代码:

string fileNameWithExtension = /*some calculation to get the full path*/
XmlSerializer serializer = new XmlSerializer(typeof(MapData));
TextWriter textWriter = new StreamWriter(fileNameWithExtension);
serializer.Serialize(textWriter, mapData);
textWriter.Close();

这是反序列化的代码:

 string fullPath = /*Retreive file's full path logic - working OK */;
 XmlSerializer deserializer = new XmlSerializer(typeof(MapData));
 FileStream fs = new FileStream(fullPath, FileMode.Open);
 mapData = null;
 mapData = (MapData)deserializer.Deserialize(fs);
 fs.Close();

我知道这很多细节,但是从我的审讯中,Exception只发生在我使用NavigationRoute poroperty的时候,所以我也会添加那些相关的类......

 public class Route
{
    public List<GeographicCoordinate> Coordinates { get; set; }

    public Route()
    {
        Coordinates = new List<GeographicCoordinate>();
    }
}

public class GeographicCoordinate
{
    public int LocationOnMap_X { get; private set; }
    public int LocationOnMap_Y { get; private set; }

    public GeographicCoordinate(Point onMap)
    {
        LocationOnMap_X = onMap.X;
        LocationOnMap_Y = onMap.Y;
    }
}

正如我之前提到的,它只是在我将一个或多个对象添加到Route的Coordinates列表之后 - 我得到了异常(这让我更加尴尬......)。另一件事我试图从GeographicCoordinate类中删除私有的setter - 但这并不好。 谢谢大家:)

1 个答案:

答案 0 :(得分:0)

您没有指定实际的例外,但我怀疑问题是您的GeographicCoordinate.LocationOnMap_XGeographicCoordinate.LocationOnMap_Y有私人设置器,您需要将其公开用于XmlSerialization。