如何将 XML 项反序列化为不同的对象?

How to deseriealize XML items as different objects?

我收到来自第三方的 XML 格式如下:

<?xml version="1.0" encoding="utf-16"?>
<ColorPages version="1">
                <Page PrintedPagePosition="0" PDFPagePosition="0" IsColor="True" />
                <Ghost PrintedPagePosition="1" PDFPagePosition="1" MayBePrinted="Unprinted" IsColor="False" />
                <Page PrintedPagePosition="2" PDFPagePosition="2" IsColor="False" />
                <Ghost PrintedPagePosition="3" PDFPagePosition="3" MayBePrinted="Unprinted" IsColor="False" />
                <Page PrintedPagePosition="4" PDFPagePosition="4" IsColor="False" />
                <Page PrintedPagePosition="5" PDFPagePosition="5" IsColor="True" />
</ColorPages>

如果我忽略 Ghost 项,我可以很容易地反序列化它,使用这些 classes:

[XmlRoot("ColorPages")]
public class ColorPages
{
    public ColorPages() { Items = new List<Page>(); }
    [XmlElement("Page")]
    public List<Page> Items { get; set; }
}

public class Page
{
    [XmlAttribute("PDFPagePosition")]
    public string PDFPagePosition { get; set; }
    [XmlAttribute("IsColor")]
    public string IsColor { get; set; }
}

现在,我知道我必须创建一个 BasePage class,它是 PageGhost 的基础 class,但是我'我不确定如何让解串器处理这个问题。

[XmlRoot("ColorPages")]
public class ColorPages
{
    [XmlElement("Ghost")]
    List<Ghost> ghost{ get; set; }

    [XmlElement("Page")]
    List<Page> page{ get; set; }
}
[XmlRoot("Ghost")]
public class Ghost
{

}
[XmlRoot("Page")]
public class Page
{

}