2010-08-15 52 views
0

我正在爲Windows Mobile 6.1(.NET CF 3.5)編寫一個小型導航應用程序,當我嘗試從文件流反序列化我的數據時出現IOException,並且我不明白爲什麼。 這裏是我的一些代碼:Deserializeling XML中的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(); 

我知道那是一個很多細節,但從我的審訊中,例外只發生在我使用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的座標列表中添加一個或多個對象之後 - 我纔會得到異常(這使得我更加尷尬)。另一件事我試圖去除地理協調類的私人定製者 - 但這並不好。 謝謝大家:)

+2

如果您收到一個您不理解的異常,* always *指定異常的內容。 – 2010-08-15 07:06:30

+0

@ET,當你說刪除私人設置者時,你的意思是你刪除了私人關鍵字還是完全刪除了設置者? XmaSerializer要求get/set都是可用的和公共的。 – 2010-08-20 05:06:15

回答

0

您沒有指定實際的例外,但我懷疑問題是您的GeographicCoordinate.LocationOnMap_XGeographicCoordinate.LocationOnMap_Y有私人設置器,您將需要這是公開的XmlSerialization。