【问题标题】:Deserialize XML CData with attribute使用属性反序列化 XML CData
【发布时间】:2016-10-19 07:16:23
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# xml xmlserializer


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-03
      • 1970-01-01
      • 2015-10-24
      相关资源
      最近更新 更多