【发布时间】:2013-12-11 17:17:52
【问题描述】:
我不会问,但我不知道又出了什么问题。 Iv 将车辆保存到 xml 文件中,当用户打开我想要反序列化的程序时。当我运行它时,我在加载方法的最后一行得到这个
'System.Windows.Markup.XamlParseException' {"'The invocation of the constructor on type 'SD2CW2.MainWindow' that matches the specified binding constraints threw an exception.'}
这些是我的加载/保存方法
private void Load()
{
XmlSerializer SerializerObj = new XmlSerializer(typeof(Vechicle));
// Reading a file requires a FileStream.
FileStream fs = new FileStream(filepath);
Vechicle = ((List<Vechicle>)SerializerObj.Deserialize(fs));
}
//Save the objects
private void Save()
{
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(filepath);
Type [] extraTypes= new Type[2];
extraTypes[0] = typeof(Tour);
extraTypes[1] = typeof(Vechicle);
// Create a new XmlSerializer instance with the type of List<Journey> and my addition types
XmlSerializer SerializerObj = new XmlSerializer(typeof(List<Journey>),extraTypes);
//serialising my journey list
SerializerObj.Serialize(WriteFileStream,Journey);
SerializerObj = new XmlSerializer(typeof(List<Vechicle>));
//serialising my vechicle list
SerializerObj.Serialize(WriteFileStream, Vechicle);
// Cleanup
WriteFileStream.Close();
}
这个xml
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfJourney xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /><?xml version="1.0" encoding="utf-8"?>
<ArrayOfVechicle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Vechicle>
<Id>1</Id>
<Registration>1</Registration>
</Vechicle>
<Vechicle>
<Id>2</Id>
<Registration>2</Registration>
</Vechicle>
<Vechicle>
<Id>3</Id>
<Registration>3</Registration>
【问题讨论】:
-
为什么要将两个不同的对象(Journey、Vehicle)序列化到同一个文件中?这些序列化操作中只有一个之后是刷新到磁盘;这可能是@splrs 答案中提到的缺少标签的原因。
-
@groverboy iv 将其更改为序列化为两个 xml 文件,标签现在正确,但我仍然无法加载。
-
如果@splrs 答案中提到的标签不正确,那么您应该接受该答案。如果您仍然无法加载,请创建一个新问题并在那里发布您更新的代码。
标签: c# xml xml-parsing