public static WeiXinMessage getMessage(string data) { WeiXinMessage wx = new WeiXinMessage(); if (data != null) { XmlDocument xml = new XmlDocument(); xml.LoadXml(data); wx.ToUserName = xml.SelectSingleNode("xml").SelectSingleNode("ToUserName").InnerText; wx.FromUserName = xml.SelectSingleNode("xml").SelectSingleNode("FromUserName").InnerText; wx.MsgType = xml.SelectSingleNode("xml").SelectSingleNode("MsgType").InnerText; if (wx.MsgType.Trim() == "text") { wx.Content = xml.SelectSingleNode("xml").SelectSingleNode("Content").InnerText; } if (wx.MsgType.Trim() == "event") { wx.EventName = xml.SelectSingleNode("xml").SelectSingleNode("Event").InnerText; wx.EventKey = xml.SelectSingleNode("xml").SelectSingleNode("EventKey").InnerText; } if (wx.MsgType.Trim() == "voice") { wx.Recognition = xml.SelectSingleNode("xml").SelectSingleNode("Recognition").InnerText; } if (wx.MsgType.Trim() == "image") { wx.MediaId = xml.SelectSingleNode("xml").SelectSingleNode("MediaId").InnerText; } } return(wx); }
public void Receive(string xml) { XElement root = XDocument.Parse(xml).Root; string value = root.Element("ToUserName").Value; string value2 = root.Element("FromUserName").Value; string value3 = root.Element("CreateTime").Value; string value4 = root.Element("MsgType").Value; string value5 = root.Element("MsgId").Value; string value6 = root.Element("AgentID").Value; WeiXinMessage weiXinMessage = new WeiXinMessage(); RoadFlow.Data.Model.WeiXinMessage weiXinMessage2 = new RoadFlow.Data.Model.WeiXinMessage(); weiXinMessage2.ID = Guid.NewGuid(); weiXinMessage2.AddTime = DateTimeNew.Now; weiXinMessage2.AgentID = value6.ToInt(); weiXinMessage2.CreateTime = value3.ToInt(); weiXinMessage2.CreateTime1 = Tools.JavaLongToDateTime(weiXinMessage2.CreateTime); weiXinMessage2.MsgType = value4; weiXinMessage2.MsgId = value5.ToLong(); weiXinMessage2.ToUserName = value; weiXinMessage2.FromUserName = value2; switch (value4) { case "text": weiXinMessage2.Contents = root.Element("Content").Value; break; case "image": weiXinMessage2.PicUrl = root.Element("PicUrl").Value; weiXinMessage2.MediaId = root.Element("MediaId").Value; break; case "voice": weiXinMessage2.Format = root.Element("Format").Value; weiXinMessage2.MediaId = root.Element("MediaId").Value; break; case "video": weiXinMessage2.ThumbMediaId = root.Element("ThumbMediaId").Value; weiXinMessage2.MediaId = root.Element("MediaId").Value; break; case "shortvideo": weiXinMessage2.ThumbMediaId = root.Element("ThumbMediaId").Value; weiXinMessage2.MediaId = root.Element("MediaId").Value; break; case "location": weiXinMessage2.Location_X = root.Element("Location_X").Value; weiXinMessage2.Location_Y = root.Element("Location_Y").Value; weiXinMessage2.Scale = root.Element("Scale").Value; weiXinMessage2.Label = root.Element("Label").Value; break; case "link": weiXinMessage2.Title = root.Element("Title").Value; weiXinMessage2.Description = root.Element("Description").Value; weiXinMessage2.PicUrl = root.Element("PicUrl").Value; break; } weiXinMessage.Add(weiXinMessage2); }