2016-10-19 137 views
0

我有一个xml文件,我尝试为它写一个类型。 在某个时候,我的大脑冻结了。使用属性反序列化XML CData

这个XML尽可能少,因为我可以写它。

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

在cs中我想写一个类作为xmlserializer的类型。

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? 
} 

不知何故选择必须是一个具有属性的类,而且选择的类型是CData。无论CData是什么,它将是一个标准类型,所以我不能设置Name属性。

如何在cs类中解决这个问题? - xml是遗留的,现在不能更改。

回答

1

你有一个良好的开端..这应该有助于你得到其余的方式。

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

+0

啊'选择选择'很高兴知道这是可能的。 – Johannes