Exemplo n.º 1
0
        // ReSharper disable PossibleNullReferenceException
        /// <summary>
        /// 根据 XML 字符串生成 RequestMessage 对象
        /// </summary>
        /// <param name="xmlString">解密后的 xml 字符串</param>
        /// <returns></returns>
        public static RequestMessage Parse(string xmlString)
        {
            var xml = XDocument.Parse(xmlString).Root;

            RequestMessageType msgType;

            if (!Enum.TryParse(xml.Element("MsgType").Value.ToLowerInvariant(), out msgType))
            {
                msgType = RequestMessageType.unknown;
            }

            var result = msgType == RequestMessageType.unknown
                ? new UnknownRequestMessage(xml)
                : msgType == RequestMessageType.@event
                ? (RequestMessage)EventMessage.Parse(xml)
                : NormalRequestMessage.Parse(xml, msgType);

            result.MsgType = msgType;

            return(result);
        }
Exemplo n.º 2
0
        // ReSharper disable PossibleNullReferenceException
        public static NormalRequestMessage Parse(XElement xml, RequestMessageType type)
        {
            NormalRequestMessage result = null;

            switch (type)
            {
            case RequestMessageType.text:
                result = new TextRequestMessage(xml);
                break;

            case RequestMessageType.image:
                result = new ImageRequestMessage(xml);
                break;

            case RequestMessageType.voice:
                result = new VoiceRequestMessage(xml);
                break;

            case RequestMessageType.video:
                result = new VideoRequestMessage(xml);
                break;

            case RequestMessageType.shortvideo:
                result = new ShortVideoRequestMessage(xml);
                break;

            case RequestMessageType.location:
                result = new LocationRequestMessage(xml);
                break;

            case RequestMessageType.link:
                result = new LinkRequestMessage(xml);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(result);
        }