2009-10-25 137 views
0

我有一个对象InputFile,它拥有数组和对象来存放文件的内容。我也有ABCFileXYZFile,它们都是从InputFile继承的,它们将读取不同类型的文件并将它们存储到InputFile的投影成员中。继承对象的XML反序列化

由于这两个对象的序列化和反序列化都与父对象相同,所以我在父对象上实现了一个标准的XML序列化接口。在反序列化过程中,读取一些参数,调用Read函数(加载文件),然后反序列化结束。

序列化的伟大工程,但(的List<InputFile>)的反序列化不起作用,因为解串器调用父母存根Read文件的功能,而不是ABCFileXYZFile的。

如何获得反序列化来识别要使用的正确对象类型?我的List<InputFile>可能有多种文件类型。

感谢

我用序列化对象的代码:

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 
} 

任何想法如何保持对象类型时,我的自定义序列化列表?

回答

7

尝试

[XmlArray] 
[XmlArrayItem(ElementName="ABCFile", Type=typeof(ABCFile))] 
[XmlArrayItem(ElementName="XYZFile", Type=typeof(XYZFile))] 
public List<InputFile> InputFileList 
{ 
    get; 
    set; 
} 

这将表明串行的是,即使这是INPUTFILE的名单,就会有将存储在这个列表中有两个派生类型。它很可能会为每个方法使用特定版本的方法。

如果失败,请告诉我。

编辑根据您的评论

我看不出这可怎么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的内容是:

<?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。

+0

感谢您的快速回复。 生成的XML仍然类似于:。 如果我手动编辑XML以将InputFile更改为ABCFile,则会失败,并显示错误的XML错误。 – Aaron 2009-10-25 15:25:58

+0

看看我的代码片段,并告诉我会发生什么 – 2009-10-25 20:25:07

+0

好吧,它认为我发现了问题的根源。我的版本的InputFileHolder有一个IXmlSerializable接口,我可以自定义读/写要串行化的参数。如果我删除了IXmlSerializable接口并让框架处理它,那么我会得到类似于你的XML,其中指定了对象类型。我需要自定义阅读,所以我需要修复写。 我会在主帖中添加我的写代码。 – Aaron 2009-10-28 23:39:54