C# url的编码解码,xml和json的序列化和反序列化

参考中国慕课网dot net web编程应用程序实践

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.Runtime.Serialization.Json;

namespace CommonFunctions
{
    public class Functions
    {
        /// <summary>
        /// 对url中待传值进行编码
        /// </summary>
        /// <param name="decodeStr">待编码的字符串</param>
        /// <returns></returns>
        public static string EncodeUrl(string decodeStr)
        {
            byte [] buf = Encoding.UTF8.GetBytes(decodeStr);
            string res = "";
            foreach(byte b in buf)
            {
                res += b.ToString("X2");
            }
            return res;
        }

        /// <summary>
        /// 对url进行解码
        /// </summary>
        /// <param name="encodeStr">待解码的字符串</param>
        /// <returns></returns>
        public static string DecodeUrl(string encodeStr)
        {
            int count = encodeStr.Length / 2;
            byte[] buf = new byte[count];
            for(int i = 0; i < count; i++)
            {
                buf[i] = byte.Parse(encodeStr.Substring(i * 2, 2),System.Globalization.NumberStyles.HexNumber);
            }
            return Encoding.UTF8.GetString(buf);
        }

        /// <summary>
        /// xml序列化
        /// </summary>
        /// <typeparam name="T">待序列化得对象类型</typeparam>
        /// <param name="obj">待序列化的对象</param>
        /// <returns></returns>
        public static string SerializerXml<T>(T obj)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            xmlSerializer.Serialize(ms, obj);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        /// <summary>
        /// xml反序列化
        /// </summary>
        /// <typeparam name="T">待反序列化的对象类型</typeparam>
        /// <param name="xmlStr">待反序列化的xml字符串</param>
        /// <returns></returns>
        public static T DeserializerXml<T>(string xmlStr)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            byte[] buf = Encoding.UTF8.GetBytes(xmlStr);
            MemoryStream ms = new MemoryStream(buf);
            return (T)xmlSerializer.Deserialize(ms);
        }

        /// <summary>
        /// json序列化
        /// </summary>
        /// <typeparam name="T">待序列化的对象类型</typeparam>
        /// <param name="obj">待序列化的对象</param>
        /// <returns></returns>
        public static string SerializerJson<T>(T obj)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
            MemoryStream ms = new MemoryStream();
            jsonSerializer.WriteObject(ms, obj);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        /// <summary>
        /// json反序列化
        /// </summary>
        /// <typeparam name="T">待反序列化的对象类型</typeparam>
        /// <param name="jsonStr">待反序列化的json字符串</param>
        /// <returns></returns>
        public static T DeserializerJson<T>(string jsonStr)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(T));
            byte[] buf = Encoding.UTF8.GetBytes(jsonStr);
            MemoryStream ms = new MemoryStream(buf);
            return (T)jsonSerializer.ReadObject(ms);
        }
    }
}
posted @ 2019-12-30 11:40  monkey6  阅读(563)  评论(0编辑  收藏  举报