问题描述
我有一个对象 InputFile
,它有数组和对象来保存文件的内容。我还有 ABCFile
和 XYZFile
,它们都是从 InputFile继承的
将读取不同类型的文件并将它们存储到 InputFile
的预计成员中。
I have an object InputFile
that has arrays and objects to hold the contents of a file. I also have ABCFile
and XYZFile
that are both inherited from InputFile
that will read different types of file and store them into the projected members of InputFile
.
由于序列化和这两个对象的反序列化与父对象相同,我在父对象上实现了标准的XML序列化接口。在反序列化期间,会读取一些参数,调用读取
函数(加载文件),然后反序列化完成。
Since the serialization and deserialization of both of these objects are identical to the parent object, I have implemented a standard XML serialization interface on the parent object. During deserialization a few paramters are read, a Read
function is called (to load a file), then the deserialization finishes.
序列化效果很好,但反序列化( List< InputFile>
)不起作用,因为反序列化器调用父节点读取
文件函数而不是 ABCFile
或 XYZFile
的。
The serialization works great, but the deserialization (of List<InputFile>
) doesn't work because the deserializer calls the parents stub Read
file function instead of ABCFile
or XYZFile
's.
如何让反序列化识别正确使用的对象类型?我的列表< InputFile>
可能会有多种文件类型。
How can I get the deserialization to recognize the correct type of object to use? It is possible that my List<InputFile>
will have a mix of file types.
谢谢
我用来序列化对象的代码:
The code I use to serialize the object:
public class InputFileHolder : IXmlSerializable {
...
public void WriteXml(XmlWriter w) {
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer ifXml = new XmlSerializer(typeof(List<InputFile>));
ifXml.Serialize(w, InputFiles, ns);
//More serialization
}
任何想法如何我自定义序列化列表时维护对象类型?
Any ideas how to maintain the object type when I custom serialize the List?
推荐答案
尝试
[XmlArray]
[XmlArrayItem(ElementName="ABCFile", Type=typeof(ABCFile))]
[XmlArrayItem(ElementName="XYZFile", Type=typeof(XYZFile))]
public List<InputFile> InputFileList
{
get;
set;
}
这将指示序列化程序,即使这是一个InputFile列表,将有两个派生类型将存储在此列表中。它可能会使每个方法使用特定版本的方法。
This will indicate the serializer that, even though this is a List of InputFile, there will be two derived types that will be stored in this list. It's likely to make it use specific version of methods for each one.
如果失败,请告诉我。
根据您的评论进行修改
我看不出这是怎么回事。
I don't see how this can be happing.
我测试了以下类:
public class InputFile
{
public String InputfileCommonProperty { get; set; }
}
public class ABCFile : InputFile
{
public String ABCSpecificProperty { get; set; }
}
public class XYZFile : InputFile
{
public String XYZSpecificProperty { get; set; }
}
public class InputFileHolder
{
public InputFileHolder()
{
InputFileList = new List<InputFile>();
}
[XmlArray]
[XmlArrayItem(ElementName = "ABCFile", Type = typeof(ABCFile))]
[XmlArrayItem(ElementName = "XYZFile", Type = typeof(XYZFile))]
public List<InputFile> InputFileList { get; set; }
}
我的主程序如下:
static void Main(string[] args)
{
InputFileHolder fileHolder = new InputFileHolder();
fileHolder.InputFileList.Add(
new ABCFile()
{
InputfileCommonProperty = "This is a common property",
ABCSpecificProperty = "This is a class specific property"
});
XmlSerializer serializer = new XmlSerializer(typeof(InputFileHolder));
MemoryStream memoryStream = new MemoryStream();
serializer.Serialize(memoryStream, fileHolder);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
String serializedString = enc.GetString(memoryStream.ToArray());
}
最后,serializedString的内容是:
And in the end, serializedString's content is:
<?xml version="1.0"?>
<InputFileHolder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<InputFileList>
<ABCFile>
<InputfileCommonProperty>This is a common property</InputfileCommonProperty>
<ABCSpecificProperty>This is a class specific property</ABCSpecificProperty>
</ABCFile>
</InputFileList>
</InputFileHolder>
你明白了吗?序列化程序知道它是ABCFile而不是通用的InputFile。
You see? The serializer knows it's a ABCFile not a generic InputFile.
这篇关于继承对象的XML反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!