<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1351776360</CreateTime> <MsgType><![CDATA[location]]></MsgType> <Location_X>23.134521</Location_X> <Location_Y>113.358803</Location_Y> <Scale>20</Scale> <Label><![CDATA[位置信息]]></Label> <MsgId>1234567890123456</MsgId> </xml>
链接消息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1351776360</CreateTime> <MsgType><![CDATA[link]]></MsgType> <Title><![CDATA[公众平台官网链接]]></Title> <Description><![CDATA[公众平台官网链接]]></Description> <Url><![CDATA[url]]></Url> <MsgId>1234567890123456</MsgId> </xml>
接上篇,看ResponseXML(postString);方法如下
/// <summary> /// 获取用户发送的消息 /// </summary> /// <param></param> private void ResponseXML(string postString) { //使用XMLDocument加载信息结构 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(postString); XmlElement rootElement = xmlDoc.DocumentElement;//获取文档的根 XmlNode MsgType = rootElement.SelectSingleNode("MsgType"); //获取消息的文本类型 RequestXML requestXML = new RequestXML();//声明实例,获取各个属性并赋值 requestXML.ToUserName = rootElement.SelectSingleNode("ToUserName").InnerText;//公众号 requestXML.FromUserName = rootElement.SelectSingleNode("FromUserName").InnerText;//用户 requestXML.CreateTime = rootElement.SelectSingleNode("CreateTime").InnerText;//创建时间 requestXML.MsgType = MsgType.InnerText;//消息类型 ///对消息的不同类型进行赋值 if (requestXML.MsgType == "text") { //赋值文本信息内容 requestXML.Content = rootElement.SelectSingleNode("Content").InnerText; } if (requestXML.MsgType.Trim() == "location") { ///赋值地理位置纬度,经度,地图缩放比例,地理位置说明 requestXML.Location_X = rootElement.SelectSingleNode("Location_X").InnerText; requestXML.Location_Y = rootElement.SelectSingleNode("Location_Y").InnerText; requestXML.Scale = rootElement.SelectSingleNode("Scale").InnerText; requestXML.Label = rootElement.SelectSingleNode("Label").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "event") { ///赋值事件名称和事件key值 requestXML.EventName = rootElement.SelectSingleNode("Event").InnerText; requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText; } if (requestXML.MsgType.Trim().ToLower() == "voice") { ///赋值语音识别结果,赋值之前一定要记得在开发者模式下,把语音识别功能开启,否则获取不到 requestXML.Recognition = rootElement.SelectSingleNode("Recognition").InnerText; } ResponseMsg(requestXML); }
语音识别功能开启如下:
requestXML是我单独创建的一个类,该类声明了消息中常用的属性字段,如下:
/// <summary> /// 接收消息的实体类 /// </summary> public class RequestXML { private String toUserName = String.Empty; /// <summary> /// 本公众号 /// </summary> public String ToUserName{get;set;} /// <summary> /// 用户微信号 /// </summary> public String FromUserName{get;set;} /// <summary> /// 创建时间 /// </summary> public String CreateTime{get;set;} /// <summary> /// 信息类型 /// </summary> public String MsgType{get;set;} /// <summary> /// 信息内容 /// </summary> public String Content{get;set;} /*以下为事件类型的消息特有的属性*/ /// <summary> /// 事件名称 /// </summary> public String EventName{get;set;} /// <summary> /// 事件值 /// </summary> public string EventKey { get; set; } /*以下为图文类型的消息特有的属性*/ /// <summary> /// 图文消息的个数 /// </summary> public int ArticleCount { get; set; } /// <summary> /// 图文消息的标题 /// </summary> public string Title { get; set; } /// <summary> /// 图文消息的简介 /// </summary> public string Description { get; set; } /// <summary> /// 图文消息图片的链接地址 /// </summary> public string PicUrl { get; set; } /// <summary> /// 图文消息详情链接地址 /// </summary> public string Url { get; set; } /// <summary> /// 图文消息集合 /// </summary> public List<RequestXML> Articles { get; set;} /*以下为地理位置类型的消息特有的属性*/ /// <summary> /// 地理位置纬度 /// </summary> public String Location_X { get; set; } /// <summary> /// 地理位置经度 /// </summary> public String Location_Y { get; set; } /// <summary> /// 地图缩放比例 /// </summary> public String Scale { get; set; } /// <summary> /// 地图位置说明 /// </summary> public String Label { get; set; } /// <summary> /// 语音消息特有字段 /// </summary> public String Recognition { get; set; } }
继续关注 ResponseMsg(requestXML);方法如下