Пример #1
0
        /// <summary>
        ///     将指定对象的指定属性列表序列化为xml文本。
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static string ToXmlText(object obj, IEnumerable <PropertyInfo> ps)
        {
            var sb       = new StringBuilder();
            var settings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true,
                ConformanceLevel   = ConformanceLevel.Fragment,
                Indent             = true,
                CloseOutput        = false
            };
            var writer = XmlWriter.Create(sb, settings);

            foreach (var p in ps)
            {
                writer.WriteStartElement(p.Name);
                //字符串返回CDATA节点
                if (p.PropertyType == typeof(string))
                {
                    writer.WriteCData(Convert.ToString(p.GetValue(obj, null)));
                }
                else if (p.PropertyType == typeof(MessageType))
                {
                    writer.WriteCData(MessageTypeAttribute.ObtainMessageType((MessageType)p.GetValue(obj, null)));
                }
                else
                {
                    writer.WriteValue(p.GetValue(obj, null));
                }
                writer.WriteEndElement();
            }
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
Пример #2
0
        /// <summary>
        /// 获取消息枚举对应的微信平台接受的消息名称
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string ObtainMessageType(MessageType type)
        {
            var mi = type.GetType().GetMember(Enum.GetName(type.GetType(), type)).FirstOrDefault();
            MessageTypeAttribute attr = null;

            if (mi != null)
            {
                attr = mi.GetCustomAttributes(typeof(MessageTypeAttribute), true).FirstOrDefault() as MessageTypeAttribute;
            }
            return(attr == null ? null : ObtainMessageType(attr));
        }
Пример #3
0
 /// <summary>
 /// 获取指定消息类型的微信平台接受名称
 /// </summary>
 /// <param name="mtype"></param>
 /// <returns></returns>
 public static string ObtainMessageType(MessageTypeAttribute mtype)
 {
     return(mtype.TypeName);
 }
Пример #4
0
 /// <summary>
 ///     获取指定消息类型的微信平台接受名称
 /// </summary>
 /// <param name="mtype"></param>
 /// <returns></returns>
 public static string ObtainMessageType(MessageTypeAttribute mtype)
 {
     return mtype.TypeName;
 }