• Xml,Json序列化


    Json到对象:

      using System.Runtime.Serialization.Json;
    
      public static T JsonAndObject<T>(string Json)
      where T : new()
      {
        DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
        using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(Json)))
        {
          T jsonObject = (T)ser.ReadObject(ms);
          return jsonObject;
        }
    
      }
    调用方式: List<T> list = AnalyzeXml.JsonAndObject<List<T>>(Json); 
    JObject Jsons = (JObject)JsonConvert.DeserializeObject(Json, Type.GetType("System.Data.UTF-8"));
    调用方式:string JsonStr=Jsons["Key"].ToString();

    对象到Json:

    public static string ObjectToJson(object obj)
      {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
        MemoryStream stream = new MemoryStream();
        serializer.WriteObject(stream, obj);
        byte[] dataBytes = new byte[stream.Length];
        stream.Position = 0;
        stream.Read(dataBytes, 0, (int)stream.Length);
        return Encoding.UTF8.GetString(dataBytes);
      }
    调用方式:String Json= ObjectToJson(T);

     Xml转对象:

      public static T Deserialize<T>(string xml) where T : new()
      {
        T t = new T();
      try
      {
      //xml = "<?xml version="1.0" encoding="utf-8" ?>" + xml;
        using (StringReader sr = new StringReader(xml))
        {
          XmlSerializer aa = new XmlSerializer(typeof(T));
          t = (T)aa.Deserialize(sr);
        }
        return t;
      }
      catch (Exception ex)
      {
        return new T();
      }
      }
    调用: T t=Deserialize<T>(Xml);

    对象到xml:

            public static string Serializer(Type type, object obj)
            {
                MemoryStream Stream = new MemoryStream();
                //创建序列化对象 
                XmlSerializer xml = new XmlSerializer(type);
                try
                {
                    //序列化对象 
                    xml.Serialize(Stream, obj);
                }
                catch (Exception ex)
                {
                    new LogManager().WriteLine("【对象转为Xml错误】:" + ex.Message + ";");
                    return "";
                }
                Stream.Position = 0;
                StreamReader sr = new StreamReader(Stream);
                string str = sr.ReadToEnd().Replace("<?xml version="1.0"?>", "").Replace(" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"", "");
                Regex regex = new Regex(@"[<]{1}[a-zA-Z]{1,30}[ ]{0,3}[/]{1}[>]{1}");
                while (regex.IsMatch(str))
                {
                    Regex regexNew = new Regex(@"[a-zA-Z]{0,20}");
                    MatchCollection matchColl = regex.Matches(str);
                    foreach (Match mc in matchColl)
                    {
                        MatchCollection match = regexNew.Matches(regex.Match(str).ToString());
                        if (mc.ToString() != "")
                        {
                            foreach (Match m in match)
                            {
                                if (m.ToString() != "")
                                {
                                    str = str.Replace(mc.ToString(), string.Format("<{0}></{0}>", m.ToString()));
                                    break;
                                }
                            }
                        }
                    }
    
                }
                return str;
            }
  • 相关阅读:
    commonJS — 数组操作(for Array)
    seafile增加邮件服务功能
    2.事务和系统概念
    1.事务处理简介
    glusterfs分布式复制扩容卷以及平衡卷
    seafile数据的备份与恢复
    seafile+glusterfs 安装部署
    虚拟机或物理服务器热添加硬盘
    bash: cannot create temp file for here-document: Read-only file system
    FATAL ERROR: please install the following Perl modules before executing ./mysql_install_db: Data::Dumper
  • 原文地址:https://www.cnblogs.com/BoyStyle/p/8966193.html
Copyright © 2020-2023  润新知