常用辅助类——JSON,XML,模型互转,MD5,Base64

时间:2025-03-28 12:54:20
  public static class ObjectHelper
    {
        #region 数据互转
        public static string ToXml(this object obj)
        {
            XmlSerializer s = new XmlSerializer(());
            using (MemoryStream ms = new MemoryStream())
            {
                using (XmlTextWriter tw = new XmlTextWriter(ms, Encoding.UTF8))
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ("", "");
                    (tw, obj, ns);
                    return Encoding.(());
                }
            }
        }
        public static T ToObjectFromXml<T>(this string xml) where T : class
        {
            XmlSerializer s = new XmlSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.(xml)))
            {
                using (StreamReader sr = new StreamReader(ms))
                {
                    return (T)(sr);
                }
            }
        }


        public static string ToJson(this object obj)
        {
            DataContractJsonSerializer s = new DataContractJsonSerializer(());
            using (MemoryStream ms = new MemoryStream())
            {
                (ms, obj);
                return Encoding.(());
            }
        }
        public static T ToObjectFromJson<T>(this string json) where T : class
        {
            DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.(json)))
            {
                return (T)(ms);
            }
        }
        public static string XmltoJson(this string xml)
        {
            XmlDocument doc = new XmlDocument();
            (xml);
            string json = (doc);
            return json;
        }
        public static string Shrink(this Guid target)
        {




            string base64 = Convert.ToBase64String(());


            string encoded = ("/", "_").Replace("+", "-");


            return (0, 22);
        }
        public static Guid ToGuid(this string target)
        {
            Guid result = ;


            if ((!(target)) && (().Length == 22))
            {
                string encoded = (().Replace("-", "+").Replace("_", "/"), "==");


                try
                {
                    byte[] base64 = Convert.FromBase64String(encoded);


                    result = new Guid(base64);
                }
                catch (FormatException)
                {
                }
            }


            return result;
        }
        public static string ToMD5(this string encyStr)
        {
            return MD5(encyStr);
        }
        public static string ToB64(this string encyStr)
        {
            return Convert.ToBase64String(("GBK").GetBytes(encyStr));
        }
        public static string FromB64(this string encyStr)
        {
            byte[] bpath1 = Convert.FromBase64String(encyStr);
            encyStr = (bpath1);
            return encyStr;
        }
    }