C# XMLSerialize 抽象继承类型

发布于 2024-11-27 00:37:10 字数 2780 浏览 1 评论 0原文

我在 C# 中进行 XML 序列化时遇到问题。

我的课程:

public abstract class AbstractFeldtyp
{
    private string _name;
    private string _beschreibung;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public string Beschreibung
    {
        get
        {
            return _beschreibung;
        }
        set
        {
            _beschreibung = value;
        }
    }
}

public class Feldtyp_A : AbstractFeldtyp {
    public Feldtyp_A() {
        Name = "My name is A";
        Beschreibung = "I'm a A";
    }
}

public class Feldtyp_B : AbstractFeldtyp {
    public Feldtyp_B() {
        Name = "My name is B";
        Beschreibung = "I'm not a A!";
    }
}

public class My_Root {
    private AbstractFeldtyp[] felder;

    public My_Root() {
        Name = "My name is B";
        Beschreibung = "I'm not a A!";
        felder = new AbstractFeldtyp[] { new Feldtyp_A(), new Feldtyp_B()};
    }
}

现在我想将“My_Root”序列化为 XML 文件。

public class Serializer
{
    private XmlSerializer s;
    private FileStream stream;
    public Serializer()
    {
        /* This is not acceptable! I must be dynamically */
        Type[] t = new Type[]
        {
            typeof(Feldtyp_A),
            typeof(Feldtyp_B)
        };
        /* End */

        s = new XmlSerializer(typeof(My_Root), t);

        AbstractMaschinenTyp m = new MaschineTyp1();
        this.SerializeObject(m);
    }

    public void SerializeObject(object obj)
    {
        stream = new FileStream(@"D:\File.xml", FileMode.Create);
        if(obj != null)
            s.Serialize(stream, obj);
        stream.Close();
    }

    public object DeserializeObject()
    {
        stream = new FileStream(@"D:\File.xml", FileMode.Open);
        object o = s.Deserialize(stream);
        stream.Close();
        return o;
    }

当然,它有效。但我在编译时不知道有哪些类...

我想要一个如下所示的 XML 文件:

<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Felder>
        <Feld>
            <Feldtyp type="Feldtyp_A" />
        </Feld>
        <Feld>
            <Feldtyp type="Feldtyp_B" />
        </Feld>
    </Felder>
</My_Root>

或者

<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Felder>
        <Feldtyp type="Feldtyp_A" />
        <Feldtyp type="Feldtyp_B" />
    </Felder>
</My_Root>

我不需要“Feldtyp_AB”中的属性。类型就够了。感谢您的帮助:)

I have a problem with the XML serialization from C#.

My classes:

public abstract class AbstractFeldtyp
{
    private string _name;
    private string _beschreibung;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    public string Beschreibung
    {
        get
        {
            return _beschreibung;
        }
        set
        {
            _beschreibung = value;
        }
    }
}

public class Feldtyp_A : AbstractFeldtyp {
    public Feldtyp_A() {
        Name = "My name is A";
        Beschreibung = "I'm a A";
    }
}

public class Feldtyp_B : AbstractFeldtyp {
    public Feldtyp_B() {
        Name = "My name is B";
        Beschreibung = "I'm not a A!";
    }
}

public class My_Root {
    private AbstractFeldtyp[] felder;

    public My_Root() {
        Name = "My name is B";
        Beschreibung = "I'm not a A!";
        felder = new AbstractFeldtyp[] { new Feldtyp_A(), new Feldtyp_B()};
    }
}

Now I want to serialize "My_Root" to a XML-File.

public class Serializer
{
    private XmlSerializer s;
    private FileStream stream;
    public Serializer()
    {
        /* This is not acceptable! I must be dynamically */
        Type[] t = new Type[]
        {
            typeof(Feldtyp_A),
            typeof(Feldtyp_B)
        };
        /* End */

        s = new XmlSerializer(typeof(My_Root), t);

        AbstractMaschinenTyp m = new MaschineTyp1();
        this.SerializeObject(m);
    }

    public void SerializeObject(object obj)
    {
        stream = new FileStream(@"D:\File.xml", FileMode.Create);
        if(obj != null)
            s.Serialize(stream, obj);
        stream.Close();
    }

    public object DeserializeObject()
    {
        stream = new FileStream(@"D:\File.xml", FileMode.Open);
        object o = s.Deserialize(stream);
        stream.Close();
        return o;
    }

Sure, it works. But i dont know at compile-time what classes are there...

I want a XML-File that looks like:

<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Felder>
        <Feld>
            <Feldtyp type="Feldtyp_A" />
        </Feld>
        <Feld>
            <Feldtyp type="Feldtyp_B" />
        </Feld>
    </Felder>
</My_Root>

or

<?xml version="1.0"?>
<My_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Felder>
        <Feldtyp type="Feldtyp_A" />
        <Feldtyp type="Feldtyp_B" />
    </Felder>
</My_Root>

I do not need the properties from "Feldtyp_AB". The the type is enough. Thanks for help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文