mlSerializer对可能属于基类或派生类的对象进行反序列化

mlSerializer对可能属于基类或派生类的对象进行反序列化

本文介绍了如何使用XmlSerializer对可能属于基类或派生类的对象进行反序列化,而无需事先知道类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,如何使用 XmlSerializer 反序列化可能是基类或多个派生类中的任何一个的对象,而无需事先知道类型?

In C#, how do I use an XmlSerializer to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type beforehand?

我所有的派生类都添加了其他数据成员。我制作了一个简单的GUI,可以序列化和反序列化类对象。它将根据用户选择填充的字段将序列化为任何合适的继承类(甚至只是基类)。

All of my derived classes add additional data members. I've made a simple GUI that can serialize and deserialize class objects. It will serialize objects as whatever inherited class (or even just the base class) is appropriate based on which fields the user chooses to populate.

序列化没有问题;问题是反序列化。 XmlSerializer 如何在不事先知道该类的情况下将数据反序列化为正确的派生类?目前,我创建了一个 XmlReader 来读取XML文件的第一个节点并从中确定类,它似乎可以满足我的目的,但是看起来非常不雅解决方案。

I have no issues with the serialization; the problem is the deserialization. How can I possibly have the XmlSerializer deserialize data to the correct derived class without knowing the class beforehand? I currently create an XmlReader to read the first node of the XML file and determine the class from it, and it seems to work for my purposes, but it seems like an extremely inelegant solution.

我在下面发布了一些示例代码。有建议吗?

I've posted some sample code below. Any suggestions?

BaseType objectOfConcern = new BaseType();
XmlSerializer xserializer;
XmlTextReader xtextreader = new XmlTextReader(DEFAULT_FILENAME);

do { xtextreader.Read(); } while (xtextreader.NodeType != XmlNodeType.Element);

string objectType = xtextreader.Name;
xtextreader.Close();

FileStream fstream = new FileStream(DEFAULT_FILENAME, FileMode.Open);

switch (objectType)
    {
case "type1":
    xserializer = new XmlSerializer(typeof(DerivedType));

    objectOfConcern = (DerivedType)xserializer.Deserialize(fstream);

    //Load fields specific to that derived type here
    whatever = (objectOfConcern as DerivedType).NoOfstreamubordinates.ToString();

    case "xxx_1":
        //code here

    case "xxx_2":
        //code here

    case "xxx_n":
        //code here

        //and so forth

    case "BaseType":
    xserializer = new XmlSerializer(typeof(BaseType));
    AssignEventHandler(xserializer);
    objectOfConcern = (BaseType)xserializer.Deserialize(fstream);
}

//Assign all deserialized values from base class common to all derived classes here

//Close the FileStream
fstream.Close();


推荐答案

有一些根类/标记,其中包含派生的类型?如果是,则可以使用映射标记名称以键入:

Have you some root class/tag which contains that derived types? If yes, you can use XmlElementAttribute to map tag name to type:

public class RootElementClass
{
    [XmlElement(ElementName = "Derived1", Type = typeof(Derived1BaseType))]
    [XmlElement(ElementName = "Derived2", Type = typeof(Derived2BaseType))]
    [XmlElement(ElementName = "Derived3", Type = typeof(Derived3BaseType))]
    public BaseType MyProperty { get; set; }
}

public class BaseType { }
public class Derived1BaseType : BaseType { }
public class Derived2BaseType : BaseType { }
public class Derived3BaseType : BaseType { }

这篇关于如何使用XmlSerializer对可能属于基类或派生类的对象进行反序列化,而无需事先知道类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:11