/// <summary> /// ResponseMessageBase.CreateFromResponseXml(xml)的扩展方法 /// 从返回结果XML转换成IResponseMessageBase实体类 /// </summary> /// <param name="xml">返回给服务器的Response Xml</param> /// <returns></returns> public static ResponseMessageBase CreateResponseMessage(this string xml) { try { if (string.IsNullOrEmpty(xml)) { return(null); } var doc = XDocument.Parse(xml); ResponseMessageBase responseMessage = null; var msgType = (ResponseMsgType)Enum.Parse(typeof(ResponseMsgType), doc.Root.Element("MsgType").Value, true); switch (msgType) { case ResponseMsgType.Text: responseMessage = new ResponseMessageText(); break; case ResponseMsgType.Image: responseMessage = new ResponseMessageImage(); break; case ResponseMsgType.Voice: responseMessage = new ResponseMessageVoice(); break; case ResponseMsgType.Video: responseMessage = new ResponseMessageVideo(); break; case ResponseMsgType.Music: responseMessage = new ResponseMessageMusic(); break; case ResponseMsgType.News: responseMessage = new ResponseMessageNews(); break; } responseMessage.FillEntityWithXml(doc); return(responseMessage); } catch (Exception ex) { throw new WeiXinException("ResponseMessageBase.CreateFromResponseXml<T>过程发生异常!" + ex.Message, ex); } }
/// <summary> /// 获取XDocument转换后的IResponseMessageBase实例(通常在反向读取日志的时候用到)。 /// 如果MsgType不存在,抛出UnknownRequestMsgTypeException异常 /// </summary> /// <returns></returns> public static IResponseMessageBase GetResponseEntity(XDocument doc) { ResponseMessageBase responseMessage = null; ResponseMsgType msgType; try { msgType = MsgTypeHelper.GetResponseMsgType(doc); switch (msgType) { case ResponseMsgType.Text: responseMessage = new ResponseMessageText(); break; case ResponseMsgType.Image: responseMessage = new ResponseMessageImage(); break; case ResponseMsgType.Voice: responseMessage = new ResponseMessageVoice(); break; case ResponseMsgType.Video: responseMessage = new ResponseMessageVideo(); break; case ResponseMsgType.News: responseMessage = new ResponseMessageNews(); break; default: throw new UnknownRequestMsgTypeException(string.Format("MsgType:{0} 在ResponseMessageFactory中没有对应的处理程序!", msgType), new ArgumentOutOfRangeException()); } responseMessage.FillEntityWithXml(doc); } catch (ArgumentException ex) { throw new WeixinException(string.Format("ResponseMessage转换出错!可能是MsgType不存在!,XML:{0}", doc), ex); } return(responseMessage); }
/// <summary> /// 从返回结果XML转换成IResponseMessageBase实体类(通常在反向读取日志的时候用到)。 /// </summary> /// <returns></returns> public static IResponseMessageBase GetResponseEntity(XDocument doc) { ResponseMessageBase val = null; try { ResponseMsgType responseMsgType = MsgTypeHelper.GetResponseMsgType(doc); switch (responseMsgType) { case ResponseMsgType.Text: val = new ResponseMessageText(); break; case ResponseMsgType.Image: val = new ResponseMessageImage(); break; case ResponseMsgType.Voice: val = new ResponseMessageVoice(); break; case ResponseMsgType.Video: val = new ResponseMessageVideo(); break; case ResponseMsgType.Music: val = new ResponseMessageMusic(); break; case ResponseMsgType.News: val = new ResponseMessageNews(); break; } val.FillEntityWithXml <ResponseMessageBase>(doc); return(val); } catch (Exception ex) { throw new Exception($"ResponseMessage转换出错!可能是MsgType不存在!,XML:{doc.ToString()}", ex); } }