[英]Trouble Serializing a Custom Collection
I have a DataGrid which holds configuration parameters for a robot I am working on. 我有一个DataGrid,其中包含我正在使用的机器人的配置参数。 I am trying to add the functionality to import/export sets of parameters, and I am trying to do so with XML Serialization.
我正在尝试添加功能来导入/导出参数集,并且我试图通过XML序列化来实现。
I am a complete novice to XML Serialization, and still pretty new to C#, so I have no idea where I am going wrong. 我是XML序列化的新手,但对于C#还是一个新手,所以我不知道我要去哪里。
I made a class, shown below, to hold the values from the data grid. 我制作了一个类,如下所示,用于保存数据网格中的值。 I could not use the same type which populates the datagrid, as there is so much disconnected logic and garbage in that class that serialization will never work.
我不能使用填充数据网格的相同类型,因为该类中有太多断开连接的逻辑和垃圾,因此序列化将永远无法进行。
[Serializable]
public class CmcdSerializationBuffer
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string TypeAssemblyQualifiedName { get; set; }
private string _storedValue;
[XmlElement]
public string StoredValue
{
get { return _storedValue; }
set { _storedValue = value; }
}
private string _temporaryValueFromUser;
public string TemporaryValueFromUser
{
get { return _temporaryValueFromUser; }
set { _temporaryValueFromUser = value; }
}
[XmlElement]
public string DefaultValue { get; set; }
public CmcdSerializationBuffer()
{
}
public CmcdSerializationBuffer(CmcdConfigurationParameter parameter)
{
Name = parameter.Name;
TypeAssemblyQualifiedName = parameter.TypeAssemblyQualifiedName;
StoredValue = parameter.StoredValue;
TemporaryValueFromUser = parameter.TemporaryValueFromUser;
DefaultValue = parameter.DefaultValue;
}
}
So I made this class to bypass that, it just takes in a CmcdConfigurationParameter
(The type populating the DataGrid) and copies the values. 因此,我做了这个类来绕过它,它只接受一个
CmcdConfigurationParameter
(填充DataGrid的类型)并复制值。
This, by the way, serializes properly. 顺便说一下,这可以正确序列化。
I want to export multiple of these into a single file, so I figured I would make a custom collection class that holds CmcdSerializatinonBuffer
objects. 我想将其中的多个导出到单个文件中,所以我想可以制作一个包含
CmcdSerializatinonBuffer
对象的自定义集合类。 Here is what I have: 这是我所拥有的:
[Serializable]
public class ValuesToExport
{
[XmlArray("ParamsList")]
public List<CmcdSerializationBuffer> Parameters { get; set; }
public ValuesToExport()
{
Parameters = new List<CmcdSerializationBuffer>();
}
public ValuesToExport(List<CmcdSerializationBuffer> parameters)
{
Parameters = new List<CmcdSerializationBuffer>();
Parameters = parameters;
}
}
And here is the function I use to export the values: 这是我用来导出值的函数:
public void Export()
{
ValuesToExport val = new ValuesToExport();
CmcdSerializationBuffer tempBuffer = new CmcdSerializationBuffer();
foreach (CmcdConfigurationParameter param in MasterDataGrid.ItemsSource)
{
tempBuffer = new CmcdSerializationBuffer(param);
val.Parameters.Add(tempBuffer);
Console.WriteLine(val.Parameters.Count());
}
MessageBox.Show(MasterDataGrid.ItemsSource.GetType().ToString());
//ValuesToExport val = new ValuesToExport(MasterDataGrid.ItemsSource);
XmlSerializer serialiser = new XmlSerializer(typeof(ValuesToExport));
using (FileStream fs = new FileStream("D:\\test\\testserialization.txt", FileMode.OpenOrCreate))
using (TextWriter writer = new StreamWriter(fs))
{
serialiser.Serialize(writer, val.Parameters);
Console.WriteLine(val.Parameters);
writer.Close();
}
}
I don't know if that is the right way to do it, but it works for a single parameter. 我不知道这是否是正确的方法,但是它适用于单个参数。 However, when I try to serialize my
ValuesToExport
class I get an error saying: 但是,当我尝试序列化
ValuesToExport
类时,出现一条错误消息:
"Unable to cast object of type 'System.Collections.Generic.List`1[Hamilton.HST.MotionController.DeviceConfiguration.CmcdSerializationBuffer]' to type 'Hamilton.HST.MotionController.DeviceUI.ValuesToExport'."
“无法将类型为'System.Collections.Generic.List`1 [Hamilton.HST.MotionController.DeviceConfiguration.CmcdSerializationBuffer]'的对象转换为类型为'Hamilton.HST.MotionController.DeviceUI.ValuesToExport'。”
And I have not been able to fix this. 而且我无法解决此问题。 If anyone knows how I can fix this, I would love to know.
如果有人知道我该如何解决,我很想知道。 Thanks!
谢谢!
You're getting this error because you created an XmlSerializer
for the type ValuesToExport
, but when you call the .Serialize
method, you're passing it a list of CmcdSerializationBuffer
. 之所以会出现此错误,是因为您为
ValuesToExport
类型创建了XmlSerializer
,但是在调用.Serialize
方法时,正在.Serialize
传递CmcdSerializationBuffer
的列表。 It's not the type the serializer was expecting, so it throws the error. 这不是序列化程序所期望的类型,因此会引发错误。
To fix it, you can change this line: 要解决此问题,您可以更改此行:
serialiser.Serialize(writer, val.Parameters);
to this: 对此:
serialiser.Serialize(writer, val);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.