62,242
社区成员




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BaseMessage info = new BaseMessage();
string result = string.Empty ;
//使用在微信平台上的图文信息(单图文信息)
ResponseNews response = new ResponseNews(info);
ArticleEntity entity = new ArticleEntity();
entity.Title = "AA";
entity.Description = "BB";
entity.PicUrl = "http://www.iqidi.com/WeixinImage/company.png";
entity.Url = "http://www.iqidi.com";
response.Articles.Add(entity);
result = response.ToXml();
Console.WriteLine(result);
}
}
/// <summary>
/// 回复图文消息
/// </summary>
[XmlRoot(ElementName = "xml")]
public class ResponseNews : BaseMessage
{
/// <summary>
/// 图文列表。
/// 多条图文消息信息,默认第一个item为大图,注意,如果图文数超过10,则将会无响应
/// </summary>
[XmlArrayItem("item")]
public List<ArticleEntity> Articles { get; set; }
public ResponseNews()
{
this.MsgType = "";
this.Articles = new List<ArticleEntity>();
}
public ResponseNews(BaseMessage info) : this()
{
this.FromUserName = info.ToUserName;
this.ToUserName = info.FromUserName;
}
/// <summary>
/// 图文消息个数,限制为10条以内
/// </summary>
public int ArticleCount
{
get
{
return this.Articles.Count;
}
set
{
;//增加这个步骤才出来XML内容
}
}
}
/// <summary>
/// 基础消息内容
/// </summary>
[XmlRoot(ElementName = "xml")]
public class BaseMessage
{
/// <summary>
/// 初始化一些内容,如创建时间为整形,
/// </summary>
public BaseMessage()
{
this.CreateTime = (DateTime.Now - (new DateTime(1970, 1, 1))).Ticks;
}
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public long CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
public string MsgType { get; set; }
public virtual string ToXml()
{
var xele = new XElement("xml", ToXmls(this));
return xele.ToString();
}
public static IEnumerable<XElement> ToXmls<T>(T item)
{
return item.GetType().GetProperties().Select(property => new XElement(property.Name, property.GetValue(item)));
}
}
public class ArticleEntity
{
public string Title { get; set; }
public string Description { get; set; }
public string PicUrl { get; set; }
public string Url { get; set; }
}
}
这段代码的输出没用包含item的子xml,请问怎么把item也包含到xml文件里面输出?