XML 序列化具有抽象基类的可序列化对象的通用列表

发布于 2025-01-04 22:14:53 字数 343 浏览 1 评论 0原文

有关如何使用抽象基类序列化通用对象列表的任何好示例。 XML Serialize可序列化对象的通用列表。我的基类类似于 Microsoft.Build.Utilities。任务

Any good sample on how to serialize list of generic objects with abstract base class. Samples with non abstract base class are listed in XML Serialize generic list of serializable objects. My base class is similar to Microsoft.Build.Utilities.Task

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潦草背影 2025-01-11 22:14:53

另一种替代方法是使用 XmlElementAttribute 将已知类型列表移动到通用列表本身......

using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

public abstract class Animal
{
    public int Weight { get; set; }    
}

public class Cat : Animal
{
    public int FurLength { get; set; }    
}

public class Fish : Animal
{
    public int ScalesCount { get; set; }    
}

public class AnimalFarm
{
    [XmlElement(typeof(Cat))]
    [XmlElement(typeof(Fish))]
    public List<Animal> Animals { get; set; }

    public AnimalFarm()
    {
        Animals = new List<Animal>();
    }
}

public class Program
{
    public static void Main()
    {
        AnimalFarm animalFarm = new AnimalFarm();
        animalFarm.Animals.Add(new Cat() { Weight = 4000, FurLength = 3 });
        animalFarm.Animals.Add(new Fish() { Weight = 200, ScalesCount = 99 });
        XmlSerializer serializer = new XmlSerializer(typeof(AnimalFarm));
        serializer.Serialize(Console.Out, animalFarm);
    }
}

这也将导致更好看的 XML 输出(没有丑陋的 xsi :type 属性)...

<?xml version="1.0" encoding="ibm850"?>
<AnimalFarm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Cat>
    <Weight>4000</Weight>
    <FurLength>3</FurLength>
  </Cat>
  <Fish>
    <Weight>200</Weight>
    <ScalesCount>99</ScalesCount>
  </Fish>
</AnimalFarm>

Another alternative is to use the XmlElementAttribute to move the list of known types to the generic list itself...

using System;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;

public abstract class Animal
{
    public int Weight { get; set; }    
}

public class Cat : Animal
{
    public int FurLength { get; set; }    
}

public class Fish : Animal
{
    public int ScalesCount { get; set; }    
}

public class AnimalFarm
{
    [XmlElement(typeof(Cat))]
    [XmlElement(typeof(Fish))]
    public List<Animal> Animals { get; set; }

    public AnimalFarm()
    {
        Animals = new List<Animal>();
    }
}

public class Program
{
    public static void Main()
    {
        AnimalFarm animalFarm = new AnimalFarm();
        animalFarm.Animals.Add(new Cat() { Weight = 4000, FurLength = 3 });
        animalFarm.Animals.Add(new Fish() { Weight = 200, ScalesCount = 99 });
        XmlSerializer serializer = new XmlSerializer(typeof(AnimalFarm));
        serializer.Serialize(Console.Out, animalFarm);
    }
}

... which will also result in a better looking XML output (without the ugly xsi:type attribute)...

<?xml version="1.0" encoding="ibm850"?>
<AnimalFarm xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Cat>
    <Weight>4000</Weight>
    <FurLength>3</FurLength>
  </Cat>
  <Fish>
    <Weight>200</Weight>
    <ScalesCount>99</ScalesCount>
  </Fish>
</AnimalFarm>
厌味 2025-01-11 22:14:53

拥有具有多个派生类型的抽象类通常很有用,以允许使用强类型列表等。

例如,您可能有一个抽象的 DocumentFragment 类和两个名为 TextDocumentFragment 和 CommentDocumentFragment 的具体类(此示例来自 Willis)。

这允许创建一个仅包含这两种类型的对象的 List 属性。

如果您尝试创建一个返回此列表的 WebService,您会收到一个错误,但这很容易通过下面的代码解决。XmlInclude

[Serializable()]
[System.Xml.Serialization.XmlInclude(typeof(TextDocumentFragment))]
[System.Xml.Serialization.XmlInclude(typeof(CommentDocumentFragment))]
public abstract class DocumentFragment {
...}

属性告诉该类它可能会序列化为这两个派生类。

这会在 DocumentFragment 元素中生成一个指定实际类型的属性,如下所示。

<DocumentFragment xsi:type="TextDocumentFragment">

任何特定于派生类的附加属性也将使用此方法包含在内。

It is often useful to have abstract classes with several derived types to allow use of strongly typed lists and the such.

For example you might have a DocumentFragment class which is abstract and two concrete classes called TextDocumentFragment and CommentDocumentFragment (this example from Willis).

This allows the creation of a List property which can contain objects only of those two types.

If you attempt to create a WebService that returns this list you get an error but this is easy to get around with the code below....

[Serializable()]
[System.Xml.Serialization.XmlInclude(typeof(TextDocumentFragment))]
[System.Xml.Serialization.XmlInclude(typeof(CommentDocumentFragment))]
public abstract class DocumentFragment {
...}

The XmlInclude attributes tell the class that it might be serialized to those two derived classes.

This generates an attribute in the DocumentFragment element specifying the actual type, as below.

<DocumentFragment xsi:type="TextDocumentFragment">

Any additonal properties specific to the derived class will also be included using this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文