【发布时间】:2013-12-11 11:54:10
【问题描述】:
Iv序列化一个List和一个List使用这种方法来分隔xml文件。
private void Save(String filePath,Type saveType)
{
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@filePath);
// Create a new XmlSerializer instance with the type of List<Journey> and my addition types
if (saveType == typeof(List<Vechicle>))
{
XmlSerializer SerializerObj = new XmlSerializer(saveType);
//serialising my vechicle list
SerializerObj.Serialize(WriteFileStream, Vechicle);
}
else
{
if (saveType == typeof(List<Journey>))
{
Type [] extraTypes= new Type[1];
extraTypes[0] = typeof(Tour);
XmlSerializer SerializerObj = new XmlSerializer(saveType,extraTypes);
SerializerObj.Serialize(WriteFileStream, Journey);
}
}
// Cleanup
WriteFileStream.Close();
}
这是 vechicls xml 文件的示例
<?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>a</Registration>
</Vechicle>
<Vechicle>
<Id>2</Id>
<Registration>b</Registration>
</Vechicle>
<Vechicle>
<Id>3</Id>
<Registration>c</Registration>
</Vechicle>
</ArrayOfVechicle>
当我尝试使用此方法将车辆日期加载回我的列表时出现问题
private void Load()
{
XmlSerializer SerializerObj = new XmlSerializer(typeof(Vechicle));
FileStream fs = new FileStream(filepath, FileMode.Open);
Vechicle = ((List<Vechicle>)SerializerObj.Deserialize(fs));
}
我在 load 方法的最后一行得到一个异常。 'System.Windows.Markup.XamlParseException'发生在 PresentationFramework.dll 'SD2CW2.MainWindow' 类型的构造函数的调用与指定的绑定约束匹配引发了异常。'
【问题讨论】:
-
'SD2CW2.MainWindow'的构造函数中是否调用了
Load()?如果是这样,您可能需要仔细检查文件路径。您是否选择了正确的文件路径?或者您可以粘贴更多异常信息。
标签: c# xml xml-parsing