Beispiel #1
0
        /// <summary>
        /// 将指定对象的指定属性列表序列化为xml文本。
        /// </summary>
        public static string ToXml(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(EnumHelper.MsgType))
                {
                    writer.WriteCData(MsgTypeAttribute.ObtainMessageType((EnumHelper.MsgType)p.GetValue(obj, null)));
                }
                else
                {
                    writer.WriteValue(p.GetValue(obj, null));
                }
                writer.WriteEndElement();
            }
            writer.Flush();
            writer.Close();
            return(sb.ToString());
        }
Beispiel #2
0
        /// <summary>
        /// 获取消息枚举对应的微信平台接受的消息名称
        /// </summary>
        public static string ObtainMessageType(EnumHelper.MsgType type)
        {
            var mi = type.GetType().GetMember(Enum.GetName(type.GetType(), type)).FirstOrDefault();
            MsgTypeAttribute attr = null;

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