BookInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyTest.Model
{
public class BookInfo
{
public int BookId { set; get; }
public string Title { set; get; }
public string Category { set; get; }
public string Author { set; get; }
public DateTime PublishDate { set; get; }
public Double Price { set; get; }
}
}
XmlHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using MyTest.Model;
using System.Xml;
namespace MyTest
{
public class XmlHelper
{
#region 方法1:使用StringBuilder拼接XML
public void CreateBookXML(string fileName)
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
List<BookInfo> bookList = GetBookList();
if (bookList != null && bookList.Count > 0)
{
xmlResult.Append("<bookstore>");
foreach (BookInfo book in bookList)
{
xmlResult.AppendFormat("<book id=\"{0}\" category=\"{1}\">", book.BookId, book.Category);
xmlResult.AppendFormat("<title>{0}</title>", book.Title);
xmlResult.AppendFormat("<author>{0}</author>", book.Author);
xmlResult.AppendFormat("<publishDate>{0}</publishDate>", book.PublishDate.ToString("yyyy-MM-dd"));
xmlResult.AppendFormat("<price>{0}</price>", book.Price);
xmlResult.Append("</book>");
}
xmlResult.Append("</bookstore>");
}
try
{
FileStream fileStream = new FileStream(fileName, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.WriteLine(xmlResult);
streamWriter.Close();
fileStream.Close();
}
catch (Exception e)
{ }
}
public string CreateBookXML()
{
StringBuilder xmlResult = new StringBuilder("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
List<BookInfo> bookList = GetBookList();
if (bookList != null && bookList.Count > 0)
{
xmlResult.Append("<bookstore>");
foreach (BookInfo book in bookList)
{
xmlResult.AppendFormat("<book id=\"{0}\" category=\"{1}\">", book.BookId, book.Category);
xmlResult.AppendFormat("<title>{0}</title>", book.Title);
xmlResult.AppendFormat("<author>{0}</author>", book.Author);
xmlResult.AppendFormat("<publishDate>{0}</publishDate>", book.PublishDate.ToString("yyyy-MM-dd"));
xmlResult.AppendFormat("<price>{0}</price>", book.Price);
xmlResult.Append("</book>");
}
xmlResult.Append("</bookstore>");
}
return xmlResult.ToString();
}
#endregion
#region 方法2:使用XmlTextWriter类创建XML
public void CreateBookXMLOther(string fileName)
{
try
{
FileStream fileStream = new FileStream(fileName, FileMode.Create);
XmlTextWriter writer = new XmlTextWriter(fileStream, Encoding.UTF8);
List<BookInfo> bookList = GetBookList();
if (bookList != null && bookList.Count > 0)
{
writer.WriteStartDocument();
writer.WriteStartElement("bookstore");
foreach (BookInfo book in bookList)
{
writer.WriteStartElement("book");
writer.WriteAttributeString("id", book.BookId.ToString());
writer.WriteAttributeString("category", book.Category);
writer.WriteStartElement("title");
writer.WriteValue(book.Title);
writer.WriteEndElement();
writer.WriteStartElement("author");
writer.WriteValue(book.Author);
writer.WriteEndElement();
writer.WriteStartElement("publishDate");
writer.WriteValue(book.PublishDate.ToString("yyyy-MM-dd"));
writer.WriteEndElement();
writer.WriteStartElement("price");
writer.WriteValue(book.Price);
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndDocument();
writer.Close();
fileStream.Close();
}
catch (Exception e)
{ }
}
#endregion
public List<BookInfo> GetBookList()
{
List<BookInfo> bookList = new List<BookInfo>();
BookInfo book1 = new BookInfo()
{
BookId = 1,
Category = "CHILDREN",
Title = "H哈利波特",
Author = "J K.罗琳",
PublishDate = new DateTime(2005, 08, 15),
Price = 29.99
};
bookList.Add(book1);
BookInfo book2 = new BookInfo()
{
BookId = 2,
Category = "WEB",
Title = "学习XML",
Author = "艾瑞克.雷",
PublishDate = new DateTime(2003, 10, 18),
Price = 39.95
};
bookList.Add(book2);
return bookList;
}
}
}
Index.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
namespace MyTest
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xmlStr= new XmlHelper().CreateBookXML();
Response.ContentType = "text/xml";
Response.Charset = "UTF-8";
Response.Write(xmlStr);
Response.End();
}
}
}
运行结果如图:
UnifiedOrder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyTest.WechatModel
{
[Serializable]
public class UnifiedOrder
{
public string appid = "";
public string mch_id = "";
public string device_info = "";
public string nonce_str = "";
public string sign = "";
public string body = "";
public string attach = "";
public string out_trade_no = "";
public int total_fee = 0;
public string spbill_create_ip = "";
public string time_start = "";
public string time_expire = "";
public string goods_tag = "";
public string notify_url = "";
public string trade_type = "";
public string openid = "";
public string product_id = "";
}
}
QueryOrder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyTest.WechatModel
{
[Serializable]
public class QueryOrder
{
public string appid = "";
public string mch_id = "";
public string transaction_id = "";
public string out_trade_no = "";
public string nonce_str = "";
public string sign = "";
}
}
OrderDetail.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyTest.WechatModel
{
[Serializable]
public class OrderDetail
{
public string return_code = "";
public string return_msg = "";
public string appid = "";
public string mch_id = "";
public string nonce_str = "";
public string sign = "";
public string result_code = "";
public string err_code = "";
public string err_code_des = "";
public string trade_state = "";
public string device_info = "";
public string openid = "";
public string is_subscribe = "";
public string trade_type = "";
public string bank_type = "";
public string total_fee = "";
public string coupon_fee = "";
public string fee_type = "";
public string transaction_id = "";
public string out_trade_no = "";
public string attach = "";
public string time_end = "";
}
}
EnterParam.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyTest.WechatModel
{
public class EnterParam
{
public bool IsAes { get; set; }
public string token { get; set; }
public string appid { get; set; }
public string EncodingAESKey { get; set; }
}
}
WechatXmlHelper.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using MyTest.Model;
using System.Xml;
using System.Collections;
using MyTest.WechatModel;
namespace MyTest
{
public class WechatXmlHelper
{
/// <summary>
/// 微信统一下单接口xml参数整理
/// </summary>
/// <param name="order">微信支付参数实例</param>
/// <param name="key">密钥</param>
/// <returns></returns>
protected string getUnifiedOrderXml(UnifiedOrder order, string key)
{
string return_string = string.Empty;
SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
sParams.Add("appid", order.appid);
sParams.Add("attach", order.attach);
sParams.Add("body", order.body);
sParams.Add("device_info", order.device_info);
sParams.Add("mch_id", order.mch_id);
sParams.Add("nonce_str", order.nonce_str);
sParams.Add("notify_url", order.notify_url);
sParams.Add("openid", order.openid);
sParams.Add("out_trade_no", order.out_trade_no);
sParams.Add("spbill_create_ip", order.spbill_create_ip);
sParams.Add("total_fee", order.total_fee.ToString());
sParams.Add("trade_type", order.trade_type);
order.sign = "0ca175b9c0f726a831d895e269332461"; //getsign(sParams, key);
sParams.Add("sign", order.sign);
//拼接成XML请求数据
StringBuilder sbPay = new StringBuilder();
foreach (KeyValuePair<string, string> k in sParams)
{
if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
{
sbPay.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">");
}
else
{
sbPay.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">");
}
}
return_string = string.Format("<xml>{0}</xml>", sbPay.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(return_string);
return_string = Encoding.GetEncoding("GBK").GetString(byteArray);
return return_string;
}
/// <summary>
/// 微信订单查询接口XML参数整理
/// </summary>
/// <param name="queryorder">微信订单查询参数实例</param>
/// <param name="key">密钥</param>
/// <returns></returns>
protected string getQueryOrderXml(QueryOrder queryorder, string key)
{
string return_string = string.Empty;
SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
sParams.Add("appid", queryorder.appid);
sParams.Add("mch_id", queryorder.mch_id);
sParams.Add("transaction_id", queryorder.transaction_id);
sParams.Add("out_trade_no", queryorder.out_trade_no);
sParams.Add("nonce_str", queryorder.nonce_str);
queryorder.sign = "0ca175b9c0f726a831d895e269332461"; //getsign(sParams, key);
sParams.Add("sign", queryorder.sign);
//拼接成XML请求数据
StringBuilder sbPay = new StringBuilder();
foreach (KeyValuePair<string, string> k in sParams)
{
if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
{
sbPay.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">");
}
else
{
sbPay.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">");
}
}
return_string = string.Format("<xml>{0}</xml>", sbPay.ToString().TrimEnd(','));
return return_string;
}
//输出XML
public string parseXML(Hashtable hashT)
{
Hashtable parameters = hashT;
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
int iIsNumber = 0;
foreach (string k in parameters.Keys)
{
string v = (string)parameters[k];
if (int.TryParse(v, out iIsNumber))
{
sb.Append("<" + k + ">" + v + "</" + k + ">");
}
else
{
sb.Append("<" + k + "><![CDATA[" + v + "]]></" + k + ">");
}
}
sb.Append("</xml>");
return sb.ToString();
}
/// <summary>
/// 返回微信POST过来的结果(微信信息返回)
/// </summary>
/// <param name="weixinXML">微信服务器post过来的数据</param>
private string ResponseMsg(string weixinXML)
{
Hashtable ht = new Hashtable(); //GetXmlName(weixinXML);
//这里写你的返回信息代码
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
sb.Append("<ToUserName><![CDATA[" + ht["FromUserName"].ToString() + "]]></ToUserName>");
sb.Append("<FromUserName><![CDATA[" + ht["ToUserName"].ToString() + "]]></FromUserName>");
sb.Append("<CreateTime>" + ht["CreateTime"].ToString() + "</CreateTime>");
sb.Append("<MsgType><![CDATA[" + ht["MsgType"].ToString() + "]]></MsgType>");
sb.Append("<Content><![CDATA[" + ht["Content"].ToString() + "]]></Content>");
sb.Append("</xml>");
return sb.ToString();
}
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
//public MsgType MsgType { get; set; }
//public void ResText(EnterParam param, string content)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.AppendFormat("<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{0}]]></Content><FuncFlag>0</FuncFlag></xml>", content);
//}
/// <summary>
/// 回复消息(音乐)
/// </summary>
//public void ResMusic(EnterParam param, Music mu)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[music]]></MsgType>");
// resxml.AppendFormat("<Music><Title><![CDATA[{0}]]></Title><Description><![CDATA[{1}]]></Description>", mu.Title, mu.Description);
// resxml.AppendFormat("<MusicUrl><![CDATA[http://{0}{1}]]></MusicUrl><HQMusicUrl><![CDATA[http://{2}{3}]]></HQMusicUrl></Music><FuncFlag>0</FuncFlag></xml>", VqiRequest.GetCurrentFullHost(), mu.MusicUrl, VqiRequest.GetCurrentFullHost(), mu.HQMusicUrl);
//}
//public void ResVideo(EnterParam param, Video v)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[video]]></MsgType>");
// resxml.AppendFormat("<Video><MediaId><![CDATA[{0}]]></MediaId>", v.media_id);
// resxml.AppendFormat("<Title><![CDATA[{0}]]></Title>", v.title);
// resxml.AppendFormat("<Description><![CDATA[{0}]]></Description></Video></xml>", v.description);
//}
/// <summary>
/// 回复消息(图片)
/// </summary>
//public void ResPicture(EnterParam param, Picture pic, string domain)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[image]]></MsgType>");
// resxml.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl></xml>", domain + pic.PictureUrl);
//}
/// <summary>
/// 回复消息(图文列表)
/// </summary>
/// <param name="param"></param>
/// <param name="art"></param>
//public void ResArticles(EnterParam param, List<Articles> art)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.AppendFormat("<MsgType><![CDATA[news]]></MsgType><ArticleCount>{0}</ArticleCount><Articles>", art.Count);
// for (int i = 0; i < art.Count; i++)
// {
// resxml.AppendFormat("<item><Title><![CDATA[{0}]]></Title> <Description><![CDATA[{1}]]></Description>", art[i].Title, art[i].Description);
// resxml.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl><Url><![CDATA[{1}]]></Url></item>", art[i].PicUrl.Contains("http://") ? art[i].PicUrl : "http://" + VqiRequest.GetCurrentFullHost() + art[i].PicUrl, art[i].Url.Contains("http://") ? art[i].Url : "http://" + VqiRequest.GetCurrentFullHost() + art[i].Url);
// }
// resxml.Append("</Articles><FuncFlag>0</FuncFlag></xml>");
//}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Text;
using MyTest.Model;
using System.Xml;
using System.Collections;
using MyTest.WechatModel;
namespace MyTest
{
public class WechatXmlHelper
{
/// <summary>
/// 微信统一下单接口xml参数整理
/// </summary>
/// <param name="order">微信支付参数实例</param>
/// <param name="key">密钥</param>
/// <returns></returns>
protected string getUnifiedOrderXml(UnifiedOrder order, string key)
{
string return_string = string.Empty;
SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
sParams.Add("appid", order.appid);
sParams.Add("attach", order.attach);
sParams.Add("body", order.body);
sParams.Add("device_info", order.device_info);
sParams.Add("mch_id", order.mch_id);
sParams.Add("nonce_str", order.nonce_str);
sParams.Add("notify_url", order.notify_url);
sParams.Add("openid", order.openid);
sParams.Add("out_trade_no", order.out_trade_no);
sParams.Add("spbill_create_ip", order.spbill_create_ip);
sParams.Add("total_fee", order.total_fee.ToString());
sParams.Add("trade_type", order.trade_type);
order.sign = "0ca175b9c0f726a831d895e269332461"; //getsign(sParams, key);
sParams.Add("sign", order.sign);
//拼接成XML请求数据
StringBuilder sbPay = new StringBuilder();
foreach (KeyValuePair<string, string> k in sParams)
{
if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
{
sbPay.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">");
}
else
{
sbPay.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">");
}
}
return_string = string.Format("<xml>{0}</xml>", sbPay.ToString());
byte[] byteArray = Encoding.UTF8.GetBytes(return_string);
return_string = Encoding.GetEncoding("GBK").GetString(byteArray);
return return_string;
}
/// <summary>
/// 微信订单查询接口XML参数整理
/// </summary>
/// <param name="queryorder">微信订单查询参数实例</param>
/// <param name="key">密钥</param>
/// <returns></returns>
protected string getQueryOrderXml(QueryOrder queryorder, string key)
{
string return_string = string.Empty;
SortedDictionary<string, string> sParams = new SortedDictionary<string, string>();
sParams.Add("appid", queryorder.appid);
sParams.Add("mch_id", queryorder.mch_id);
sParams.Add("transaction_id", queryorder.transaction_id);
sParams.Add("out_trade_no", queryorder.out_trade_no);
sParams.Add("nonce_str", queryorder.nonce_str);
queryorder.sign = "0ca175b9c0f726a831d895e269332461"; //getsign(sParams, key);
sParams.Add("sign", queryorder.sign);
//拼接成XML请求数据
StringBuilder sbPay = new StringBuilder();
foreach (KeyValuePair<string, string> k in sParams)
{
if (k.Key == "attach" || k.Key == "body" || k.Key == "sign")
{
sbPay.Append("<" + k.Key + "><![CDATA[" + k.Value + "]]></" + k.Key + ">");
}
else
{
sbPay.Append("<" + k.Key + ">" + k.Value + "</" + k.Key + ">");
}
}
return_string = string.Format("<xml>{0}</xml>", sbPay.ToString().TrimEnd(','));
return return_string;
}
//输出XML
public string parseXML(Hashtable hashT)
{
Hashtable parameters = hashT;
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
int iIsNumber = 0;
foreach (string k in parameters.Keys)
{
string v = (string)parameters[k];
if (int.TryParse(v, out iIsNumber))
{
sb.Append("<" + k + ">" + v + "</" + k + ">");
}
else
{
sb.Append("<" + k + "><![CDATA[" + v + "]]></" + k + ">");
}
}
sb.Append("</xml>");
return sb.ToString();
}
/// <summary>
/// 返回微信POST过来的结果(微信信息返回)
/// </summary>
/// <param name="weixinXML">微信服务器post过来的数据</param>
private string ResponseMsg(string weixinXML)
{
Hashtable ht = new Hashtable(); //GetXmlName(weixinXML);
//这里写你的返回信息代码
StringBuilder sb = new StringBuilder();
sb.Append("<xml>");
sb.Append("<ToUserName><![CDATA[" + ht["FromUserName"].ToString() + "]]></ToUserName>");
sb.Append("<FromUserName><![CDATA[" + ht["ToUserName"].ToString() + "]]></FromUserName>");
sb.Append("<CreateTime>" + ht["CreateTime"].ToString() + "</CreateTime>");
sb.Append("<MsgType><![CDATA[" + ht["MsgType"].ToString() + "]]></MsgType>");
sb.Append("<Content><![CDATA[" + ht["Content"].ToString() + "]]></Content>");
sb.Append("</xml>");
return sb.ToString();
}
/// <summary>
/// 开发者微信号
/// </summary>
public string ToUserName { get; set; }
/// <summary>
/// 发送方帐号(一个OpenID)
/// </summary>
public string FromUserName { get; set; }
/// <summary>
/// 消息创建时间 (整型)
/// </summary>
public string CreateTime { get; set; }
/// <summary>
/// 消息类型
/// </summary>
//public MsgType MsgType { get; set; }
//public void ResText(EnterParam param, string content)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.AppendFormat("<MsgType><![CDATA[text]]></MsgType><Content><![CDATA[{0}]]></Content><FuncFlag>0</FuncFlag></xml>", content);
//}
/// <summary>
/// 回复消息(音乐)
/// </summary>
//public void ResMusic(EnterParam param, Music mu)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[music]]></MsgType>");
// resxml.AppendFormat("<Music><Title><![CDATA[{0}]]></Title><Description><![CDATA[{1}]]></Description>", mu.Title, mu.Description);
// resxml.AppendFormat("<MusicUrl><![CDATA[http://{0}{1}]]></MusicUrl><HQMusicUrl><![CDATA[http://{2}{3}]]></HQMusicUrl></Music><FuncFlag>0</FuncFlag></xml>", VqiRequest.GetCurrentFullHost(), mu.MusicUrl, VqiRequest.GetCurrentFullHost(), mu.HQMusicUrl);
//}
//public void ResVideo(EnterParam param, Video v)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[video]]></MsgType>");
// resxml.AppendFormat("<Video><MediaId><![CDATA[{0}]]></MediaId>", v.media_id);
// resxml.AppendFormat("<Title><![CDATA[{0}]]></Title>", v.title);
// resxml.AppendFormat("<Description><![CDATA[{0}]]></Description></Video></xml>", v.description);
//}
/// <summary>
/// 回复消息(图片)
/// </summary>
//public void ResPicture(EnterParam param, Picture pic, string domain)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.Append(" <MsgType><![CDATA[image]]></MsgType>");
// resxml.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl></xml>", domain + pic.PictureUrl);
//}
/// <summary>
/// 回复消息(图文列表)
/// </summary>
/// <param name="param"></param>
/// <param name="art"></param>
//public void ResArticles(EnterParam param, List<Articles> art)
//{
// StringBuilder resxml = new StringBuilder(string.Format("<xml><ToUserName><![CDATA[{0}]]></ToUserName><FromUserName><![CDATA[{1}]]></FromUserName><CreateTime>{2}</CreateTime>", FromUserName, ToUserName, Utils.ConvertDateTimeInt(DateTime.Now)));
// resxml.AppendFormat("<MsgType><![CDATA[news]]></MsgType><ArticleCount>{0}</ArticleCount><Articles>", art.Count);
// for (int i = 0; i < art.Count; i++)
// {
// resxml.AppendFormat("<item><Title><![CDATA[{0}]]></Title> <Description><![CDATA[{1}]]></Description>", art[i].Title, art[i].Description);
// resxml.AppendFormat("<PicUrl><![CDATA[{0}]]></PicUrl><Url><![CDATA[{1}]]></Url></item>", art[i].PicUrl.Contains("http://") ? art[i].PicUrl : "http://" + VqiRequest.GetCurrentFullHost() + art[i].PicUrl, art[i].Url.Contains("http://") ? art[i].Url : "http://" + VqiRequest.GetCurrentFullHost() + art[i].Url);
// }
// resxml.Append("</Articles><FuncFlag>0</FuncFlag></xml>");
//}
}
}
WechatXml.aspx.cs
using System
using System.Collections.Generic
using System.Linq
using System.Web
using System.Web.UI
using System.Web.UI.WebControls
using System.Collections
namespace MyTest
{
public partial class WechatXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Hashtable ht = new Hashtable()
ht.Add("Name", "wu")
ht.Add("Age", "27")
ht.Add("Country", "American")
ht.Add("Job", "NET高级开发")
ht.Add("Interesting", "电影,书籍")
WechatXmlHelper helper = new WechatXmlHelper()
string xmlStr = helper.parseXML(ht)
Response.ContentType = "text/xml"
Response.Charset = "UTF-8"
Response.Write(xmlStr)
Response.End()
}
}
}
运行结果如图: