C# 反序列化XML上的IOException
我正在为Windows Mobile 6.1(.NET CF 3.5)编写一个小型导航应用程序,当我尝试从文件流中反序列化数据时,遇到IOException,我不明白为什么。 以下是我的一些代码:C# 反序列化XML上的IOException,c#,xml-serialization,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;
//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属性时才会出现异常,所以我还会添加那些相关的类
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;
}
}
公共类路由
{
公共列表坐标{get;set;}
公共路线()
{
坐标=新列表();
}
}
公共类地理坐标
{
public int locationMap_X{get;private set;}
public int locationmap_Y{get;private set;}
公共地理坐标(地图上的点)
{
位置onMap_X=onMap.X;
位置onMap_Y=onMap.Y;
}
}
正如我前面提到的,只有在我将一个或多个对象添加到路线的坐标列表中之后,我才会得到异常(这使我更加尴尬…)。另一件事是,我试图将私有setter从GeographicCoordinate类中删除,但没有效果。
谢谢大家:)您没有指定实际的异常,但我怀疑问题在于您的
地理坐标.locationMap\u X
和地理坐标.locationMap\u Y
有私有设置器,您需要将其公开用于XmlSerialization。如果您遇到不理解的异常,始终指定异常内容中的内容。@ET,当您说删除专用设置程序时,是指删除了专用关键字还是完全删除了设置程序?XmaSerializer要求get/set都是可用的和公共的。