定义一个可序列化的类
/// <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; }