c#序列化

定义一个可序列化的类
/// <summary>
///Music 的摘要说明
/// </summary>
[Serializable]
public class Music
{
    // 音乐消息名称
    [XmlIgnore]
    public String Title
    {
        get;
        set;
    }

    [XmlElement("Title")]
    public XmlNode[] TitleNodes
    {
        get
        {
            var dom = new XmlDocument();
            return new XmlNode[] { dom.CreateCDataSection(this.Title) };
        }
        set
        {
            if (value == null)
            {
                this.Title = null;
                return;
            }

            if (value.Length != 1)
                throw new InvalidOperationException("Invalid array.");
            var content = value[0];
            if (null == content)
                throw new InvalidOperationException("Node is null.");
            this.Title = content.Value;
        }

    }

    // 音乐消息描述
    [XmlIgnore]
    public String Description
    {
        get;
        set;
    }

    [XmlElement("Description")]
    public XmlNode[] DescriptionNodes
    {
        get
        {
            var dom = new XmlDocument();
            return new XmlNode[] { dom.CreateCDataSection(this.Description) };
        }
        set
        {
            if (value == null)
            {
                this.Description = null;
                return;
            }

            if (value.Length != 1)
                throw new InvalidOperationException("Invalid array.");
            var content = value[0];
            if (null == content)
                throw new InvalidOperationException("Node is null.");
            this.Description = content.Value;
        }

    }

    // 音乐链接  
    [XmlIgnore]
    public String MusicUrl
    {
        get;
        set;
    }
    [XmlElement("MusicUrl")]
    public XmlNode[] MusicUrlNodes
    {
        get
        {
            var dom = new XmlDocument();
            return new XmlNode[] { dom.CreateCDataSection(this.MusicUrl) };
        }
        set
        {
            if (value == null)
            {
                this.MusicUrl = null;
                return;
            }

            if (value.Length != 1)
                throw new InvalidOperationException("Invalid array.");
            var content = value[0];
            if (null == content)
                throw new InvalidOperationException("Node is null.");
            this.MusicUrl = content.Value;
        }

    }

    // 高质量音乐链接,WIFI环境优先使用该链接播放音乐
    [XmlIgnore]
    public String HQMusicUrl
    {
        get;
        set;
    }
    [XmlElement("HQMusicUrl")]
    public XmlNode[] HQMusicUrlNodes
    {
        get
        {
            var dom = new XmlDocument();
            return new XmlNode[] { dom.CreateCDataSection(this.HQMusicUrl) };
        }
        set
        {
            if (value == null)
            {
                this.HQMusicUrl = null;
                return;
            }

            if (value.Length != 1)
                throw new InvalidOperationException("Invalid array.");
            var content = value[0];
            if (null == content)
                throw new InvalidOperationException("Node is null.");
            this.HQMusicUrl = content.Value;
        }

    }
}


   [XmlIgnore]代表这个属性不可被序列化,而像下面的代码则是为了将属性的值放在CDATA块中

   // 音乐链接  
    [XmlIgnore]
    public String MusicUrl
    {
        get;
        set;
    }
    [XmlElement("MusicUrl")]
    public XmlNode[] MusicUrlNodes
    {
        get
        {
            var dom = new XmlDocument();
            return new XmlNode[] { dom.CreateCDataSection(this.MusicUrl) };
        }
        set
        {
            if (value == null)
            {
                this.MusicUrl = null;
                return;
            }

            if (value.Length != 1)
                throw new InvalidOperationException("Invalid array.");
            var content = value[0];
            if (null == content)
                throw new InvalidOperationException("Node is null.");
            this.MusicUrl = content.Value;
        }

    }





使用XmlSerializer序列化对象,返回xml格式的字符串

    /// <summary>
    /// 对回复消息的序列化(XML)    
    /// </summary>
    public static string ParseMessage(RepBaseMessage msg)
    {
        StringBuilder sb = new StringBuilder();
        StringWriter w = new StringWriter(sb);
        XmlSerializer sr = new XmlSerializer(msg.GetType());
        sr.Serialize(w, msg);

        w.Close();
        String xml = sb.ToString();
        if (msg.MsgType == "news")
        {
            xml = xml.Replace("<Article>", "<item>");
            xml = xml.Replace("</Article>", "</item>");
        }
        return xml;

    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值