/// <summary> /// Xml序列化与反序列化 示例 /// </summary> private static void XmlHelperTest() { //示例类型 var t1 = new testEntity(); t1.name = "test1"; t1.password = "******"; t1.Age = 18; t1.now = DateTime.Now; t1.ip = new IP(); t1.ip.host = "rock"; t1.ip.ipv4 = "192.168.1.6"; var t2 = new testEntity(); t2.name = "test2"; t2.password = "******"; t2.Age = 18; t2.now = DateTime.Now; t2.ip = null; var list = new List <testEntity>(); list.Add(t1); list.Add(t2); //Xml序列化 var xmlt = t1.GetXmlString(); var xmlt1 = t1.GetXmlString(Encoding.UTF8); var xmlt2 = t2.GetXmlString(Encoding.UTF8, true); var xmlt3 = t2.GetXmlString(Encoding.UTF8, true, false); var xmlt4 = t2.GetXmlString(Encoding.UTF8, true, false, true); var xmlt_1 = list.GetXmlString(); //Xml反序列化 var ttt1 = xmlt1.GetXmlObj <testEntity>(); var ttt2 = xmlt2.GetXmlObj <testEntity>(); var ttt3 = xmlt3.GetXmlObj <testEntity>(); var ttt4 = xmlt4.GetXmlObj <testEntity>(); var ttt_1 = xmlt_1.GetXmlObj <List <testEntity> >(); //GBK格式Xml序列化 var xmlf1 = t1.GetGBKXmlString(); var xmlf2 = t1.GetGBKXmlString(Encoding.UTF8); var xmlf4 = t2.GetXmlString(Encoding.UTF8, true, false, true); //DataTable示例类型 DataTable dt = new DataTable(); dt.TableName = "test1"; //xmlHelper操作DataTable时必须有表名否则会报错 dt.Columns.Add("Name", typeof(string)); //数据类型为 文本 DataColumn age = new DataColumn("Age", typeof(Int32)); //数据类型为 整形 DataColumn Time = new DataColumn("Time", typeof(DateTime)); //数据类型为 时间 dt.Columns.Add(age); dt.Columns.Add(Time); DataRow dr1 = dt.NewRow(); dr1[0] = "张三"; //通过索引赋值 dr1[1] = 23; dr1["Time"] = DateTime.Now; //通过名称赋值 dt.Rows.Add(dr1); dt.Rows.Add("李四", 25, DateTime.Now); //Add你们参数的数据顺序要和dt中的列顺对应 //DateTable的Xml序列化和反序列化 var dtxml = dt.GetXmlString(); var dt2 = dtxml.GetXmlObj <DataTable>(); }
/// <summary> /// Json序列化与反序列化 示例 /// </summary> private static void JsonHelperTest() { //基础示例类型 var t1 = new testEntity(); t1.name = "test1"; t1.password = "******"; t1.Age = 18; t1.now = DateTime.Now; t1.ip = new IP(); t1.ip.host = "rock"; t1.ip.ipv4 = "192.168.1.6"; var t2 = new testEntity(); t2.name = "test2"; t2.password = "******"; t2.Age = 18; t2.now = DateTime.Now; t2.ip = null; var list = new List <testEntity>(); list.Add(t1); list.Add(t2); //基础示例类型序列化与反序列化 var s1 = t1.JSONSerialize(); var s2 = t2.JSONSerialize(); var s3 = list.JSONSerialize(false); var d1 = s1.JSONDeserialize <testEntity>(); testEntity d2 = s2.JSONDeserialize <testEntity>(); var d3 = s3.JSONDeserialize <testEntity>();//null:类型不对应 List <testEntity> b2 = s3.JSONDeserialize <List <testEntity> >(); //匿名类型及其序列化 var obj = new { name = "123", pas = "******", a = new { ip = "127.0.0.1", date = DateTime.Now } }; var st = obj.JSONSerialize(); //DataTable示例类型 DataTable dt = new DataTable(); dt.Columns.Add("Name", typeof(string)); //数据类型为 文本 DataColumn age = new DataColumn("Age", typeof(Int32)); //数据类型为 整形 DataColumn Time = new DataColumn("Time", typeof(DateTime)); //数据类型为 时间 dt.Columns.Add(age); dt.Columns.Add(Time); DataRow dr1 = dt.NewRow(); dr1[0] = "张三"; //通过索引赋值 dr1[1] = 23; dr1["Time"] = DateTime.Now; //通过名称赋值 dt.Rows.Add(dr1); dt.Rows.Add("李四", 25, DateTime.Now); //Add你们参数的数据顺序要和dt中的列顺对应 //DataTable示例类型序列化与反序列化 var s4 = dt.JSONSerialize(); var a3 = s4.JSONDeserialize <DataTable>(); //OK var a5 = s4.JSONDeserialize <List <Dictionary <string, object> > >(); //OK var a4 = s4.JSONDeserializeDynamic(); //dynamic {object[]} //反序列化为动态类型时取值操作示例 var result = ""; var m1 = s1.JSONDeserializeDynamic();//dynamic {Dictionary<string,object>} if (((Dictionary <string, object>)m1).ContainsKey("name")) { result += m1["name"].ToString(); //test1 } var m2 = s3.JSONDeserializeDynamic(); //dynamic {object[]} if (m2.Length > 0 && m2[0].Count > 0 && ((Dictionary <string, object>)m2[0]).ContainsKey("name")) //m2[0] dynamic {Dictionary<string,object>} { result += m2[0]["name"].ToString(); //test1 } }