如何将XML项目作为不同的对象进行deseriealize?

时间:2018-01-23 14:25:21

标签: c# xml-deserialization

我正在使用以下格式从第三方接收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项,我可以轻松地反序列化:

[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类,它是PageGhost的基类,但我不知道如何制作反序列化器处理这个。

1 个答案:

答案 0 :(得分:1)

with cte as (
  select goal_id, parent_goal_id, goal_name,
    row_number() over (partition by parent_goal_id order by goal_id) as rn
  from goals
)
select ltrim(sys_connect_by_path(rn, '.'), '.') as label,
  goal_name
from cte
start with parent_goal_id is null
connect by parent_goal_id = prior goal_id;