Exemplo n.º 1
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  会使用Table名称作为节点名,如果再为空 则去类型作为节点名</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            JsonData jsonData = nodeValue as JsonData;

            if (string.IsNullOrEmpty(nodeName))
            {
                nodeName = nodeValue.GetType().Name;//取类型作为节点的名称
            }

            tw.WriteLine(string.Format("<{0}>", nodeName));

            if (jsonData != null)
            {
                //在JsonData不为空的情况下才可以
                switch (jsonData.GetJsonType())
                {
                    case JsonType.Object:
                        #region 如果是字典类
                        //如果是字典类
                        IDictionary<string, JsonData> idictJsonData = jsonData.Inst_Object;
                        foreach (string key in idictJsonData.Keys)
                        {
                            //遍历 字典的单个进行序列化
                            XmlSerializerContext dictContext = new XmlSerializerContext(tw, idictJsonData[key].GetValueType());
                            dictContext.Serialize(key, idictJsonData[key]);
                        }
                        #endregion
                        break;
                    case JsonType.Array:
                        #region 如果是数组类型  因为数组里面还是JsonData  所以还需要交还给自己处理
                        tw.WriteLine(string.Format("<{0}>", "array"));
                        for (int i = 0, count = jsonData.Count; i < count; i++)
                        {
                            XmlSerializerContext arrayContext = new XmlSerializerContext(tw, typeof(JsonData));
                            arrayContext.Serialize("item", jsonData[i]);
                        }
                        tw.WriteLine(string.Format("</{0}>", "array"));
                        #endregion
                        break;
                    default:
                        #region 直接是最普通的类型
                        XmlSerializerContext context = new XmlSerializerContext(tw, jsonData.GetValueType());
                        context.Serialize(jsonData.GetValueType().Name, jsonData);
                        #endregion
                        break;

                }
            }

            tw.WriteLine(string.Format("</{0}>", nodeName));
        }
Exemplo n.º 2
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  会使用Table名称作为节点名,如果再为空 则去类型作为节点名</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            DataTable dt=nodeValue as DataTable;

            if (string.IsNullOrEmpty(nodeName))
            {
                if (dt != null && !string.IsNullOrEmpty(dt.TableName))
                {
                    nodeName = dt.TableName;//去表名作为节点名称
                }
                else {
                    nodeName = nodeValue.GetType().Name;//取类型作为节点的名称
                }

            }

            tw.WriteLine(string.Format("<{0}>", nodeName));

            if (dt != null && dt.Rows.Count > 0)
            {
                //DataTable里面有数据
                DataColumnCollection columns = dt.Columns;//取列名
                for (int i = 0, count = dt.Rows.Count; i < count; i++)
                {
                    tw.WriteLine(string.Format("<{0}>", "row"));//添加行标志
                    //遍历行
                    foreach (DataColumn dc in columns)
                    {
                        //遍历单元格
                        //遍历 进行单个的序列化
                        XmlSerializerContext context = new XmlSerializerContext(tw, columns[dc.ColumnName].DataType);
                        context.Serialize(dc.ColumnName, dt.Rows[i][dc.ColumnName]);
                    }
                    tw.WriteLine(string.Format("</{0}>", "row"));
                }
            }

            tw.WriteLine(string.Format("</{0}>", nodeName));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  使用类型作为节点的名称</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            if (string.IsNullOrEmpty(nodeName))
            {
                nodeName = nodeValue.GetType().Name;
            }

            tw.WriteLine(string.Format("<{0}>", nodeName));
            //得到该实体类的属性
            PropertyInfo[] propertys = nodeValue.GetType().GetProperties(_indingFlags);
            for (int i = 0; i < propertys.Length; i++)
            {
                //遍历 进行属性进行序列化
                XmlSerializerContext context = new XmlSerializerContext(tw, propertys[i].PropertyType);
                context.Serialize(propertys[i].Name, propertys[i].GetValue(nodeValue, null));
            }

            tw.WriteLine(string.Format("</{0}>", nodeName));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 得到输出的数据
        /// </summary>
        /// <param name="obj">要输出的值</param>
        /// <param name="type">要输出的类型</param>
        /// <returns>返回Json字符串</returns>
        public override string GetResponse(object obj, Type type)
        {
            base.GetResponse(obj, type);

            if (type == typeof(object))
            {
                //如果类型是object  尝试取他实际的类型
                type = obj.GetType();
            }

            StringBuilder sb = new StringBuilder();//字符流
            TextWriter writer = new StringWriter(sb);//IO写的载体
            XmlSerializerContext serializerContext;//序列化对象
            try
            {
                //声明Xml序列化对象实例serializer
                serializerContext = new XmlSerializerContext(writer, type);
                //执行序列化并将序列化结果输出到writer
                serializerContext.Serialize(obj);
            }
            catch (Exception ex)
            {
                writer.Close();
                sb = new StringBuilder();
                writer = new StringWriter(sb);
                serializerContext = new XmlSerializerContext(writer, type);
                serializerContext.Serialize(ex.Message);
            }
            finally
            {
                //关闭当前写的IO
                writer.Close();
            }

            return sb.ToString();
        }
Exemplo n.º 5
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  使用类型作为节点的名称</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            if (string.IsNullOrEmpty(nodeName))
            {
                nodeName = nodeValue.GetType().Name;
            }

            IList list = nodeValue as IList;//先转为接口可以读取的类型
            tw.WriteLine("<{0}>", nodeName);
            if (list != null && list.Count>0)
            {
                //取得了正常转换的泛型的值

                foreach (var obj in list)
                {
                    //遍历 进行单个的序列化
                    XmlSerializerContext context = new XmlSerializerContext(tw, obj.GetType());
                    context.Serialize("item", obj);
                }
            }
            tw.WriteLine("</{0}>", nodeName);
        }
Exemplo n.º 6
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  使用类型作为节点的名称</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            if (string.IsNullOrEmpty(nodeName))
            {
                nodeName = nodeValue.GetType().Name;
            }

            tw.WriteLine(string.Format("<{0}>", nodeName));
            Hashtable hs = nodeValue as Hashtable;
            if (hs != null)
            {
                //取得了正常转换的Hsh的值

                foreach (DictionaryEntry de in hs)
                {
                    //遍历 进行单个的序列化
                    XmlSerializerContext context = new XmlSerializerContext(tw, de.Value.GetType());
                    context.Serialize(Convert.ToString(de.Key), de.Value);
                }
            }
            tw.WriteLine(string.Format("</{0}>", nodeName));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 进行序列化操作
        /// </summary>
        /// <param name="tw"></param>
        /// <param name="nodeName">序列化出来节点的名称  如果值为空的话  使用类型作为节点的名称</param>
        /// <param name="nodeValue">需要序列化节点的值</param>
        public override void Serialize(TextWriter tw, string nodeName, object nodeValue)
        {
            base.Serialize(tw, nodeName, nodeValue);

            Type t = nodeValue.GetType();//获取节点的类型

            Type[] ts = t.GetGenericArguments();//取得泛型类型的参数列表

            if (string.IsNullOrEmpty(nodeName))
            {
                nodeName = nodeValue.GetType().Name;
            }

            tw.WriteLine(string.Format("<{0}>", nodeName));
            if (ts.Length >=2)
            {
                //在类型2个以上才会进行操作
                if (typeof(string).Equals(ts[0]))
                { //字典键一定要是字符串类型  否则不操作

                    PropertyInfo keys = t.GetProperty("Keys");//取得键的属性
                    MethodInfo mi;//获取值的方法申明

                    #region 获取方法
                    if (_methodInfoCache.ContainsKey(t))
                    {
                        mi = _methodInfoCache[t];
                    }
                    else {
                        mi = nodeValue.GetType().GetMethod("TryGetValue");//取得获取值的方法
                        _methodInfoCache.Add(t, mi);//把方法添加进缓存
                    }
                    #endregion

                    IEnumerable ieKeys = keys.GetValue(nodeValue, null) as IEnumerable;//取得键的集合

                    foreach (string key in ieKeys)
                    {
                        object[] args = new object[2] { key, null };//参数的申明
                        object result = mi.Invoke(nodeValue, args);
                        if (Convert.ToBoolean(result))
                        {
                            //方法执行成功
                            //遍历 进行单个的序列化
                            XmlSerializerContext context = new XmlSerializerContext(tw, args[1].GetType());
                            context.Serialize(key, args[1]);
                        }

                    }
                }
            }
            tw.WriteLine(string.Format("</{0}>", nodeName));
        }