Beispiel #1
0
        /// <summary>
        /// 将微信post过来的xml字符串转换为实体
        /// </summary>
        /// <param name="rootElement"></param>
        /// <returns></returns>
        private static RequestXML GetRequestXML(XmlElement rootElement)
        {
            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;

                var tempInt = 0L;
                long.TryParse(rootElement.SelectSingleNode("MsgId").InnerText, out tempInt);
                requestXML.MsgId = tempInt;
            }
            else if (requestXML.MsgType == "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;
            }
            else if (requestXML.MsgType == "event")
            {
                requestXML.EventType = rootElement.SelectSingleNode("Event").InnerText;
                if (requestXML.EventType == "CLICK")
                {
                    requestXML.Content  = rootElement.SelectSingleNode("EventKey").InnerText;
                    requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
                }
                else if (requestXML.EventType.ToLower() == "subscribe")
                {
                    requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
                }
                else if (requestXML.EventType.ToLower() == "scan")
                {
                    requestXML.EventKey = rootElement.SelectSingleNode("EventKey").InnerText;
                }
            }
            else if (requestXML.MsgType == "image")
            {
                requestXML.PicUrl = rootElement.SelectSingleNode("PicUrl").InnerText;
            }

            return(requestXML);
        }
Beispiel #2
0
        /// <summary>
        /// 将微信post过来的信息转换为实体
        /// </summary>
        /// <param name="rootElement"></param>
        /// <returns></returns>
        public static RequestXML GetRequestXML(Stream s)
        {
            RequestXML result = null;

            try
            {
                byte[] b = new byte[s.Length];
                s.Read(b, 0, (int)s.Length);
                var postStr = Encoding.UTF8.GetString(b);
                if (!string.IsNullOrEmpty(postStr))
                {
                    var doc = new XmlDocument();
                    doc.LoadXml(postStr);
                    XmlElement rootElement = doc.DocumentElement;
                    result = GetRequestXML(rootElement);
                }
                return(result);
            }
            catch
            {
                return(result);
            }
        }