使用属性反序列化 XML CData

Deserialize XML CData with attribute

我有一个 xml 文件,我尝试为它写一个类型。 在某个时刻我的大脑冻结了。

这个xml是我能写的最简单的。

<Level ID="SomeID">
    <Selection Name="AnotherID">
        <Content><![CDATA[SomeData]]></Content>
    </Selection>
</Level>

在 cs 中我想写一个 class 作为 xml 序列化器的类型。

public class Level
{
    [XmlAttribute]
    public string ID {get; set;}
    public ??? Selection {get; set;}
    //What is the type of CDATA
    //Where would the Name Attribute go?
}

Selection 必须是具有属性的 class,而且 Selection 的类型是 CData。无论 CData 是什么,它都是标准类型,因此我无法设置 Name 属性。

如何在 cs class 中解决这个问题? - xml 是遗留的,现在无法更改。

你有一个好的开始..这应该可以帮助你完成剩下的事情。

public class Level
{
    [XmlAttribute]
    public string ID {get; set;}
    public Selection Selection {get; set;}
}

public class Selection {
    [XmlAttribute]
    public string Name {get;set;}
    public Content Content {get;set;}
}

public class Content {
    [XmlText]
    public string Data {get;set;}
}

因此,要通过您的对象模型访问该 CDATA 文本,您将访问 Level.Selection.Content.Data