基础类库--E-commerce
Posted youguess
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基础类库--E-commerce相关的知识,希望对你有一定的参考价值。
一般用到的基础类库
1.XML的序列化和反序列化
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Serialization; namespace EC_System.FrameWork.API { public static class SerializeHelper { public static string Serialize<T>(T instance) { XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); //Add an empty namespace and empty value ns.Add("", ""); XmlWriterSettings settings = new XmlWriterSettings(); // Remove the <?xml version="1.0" encoding="utf-8"?> settings.OmitXmlDeclaration = true; // settings.DoNotEscapeUriAttributes = false; settings.Indent = true; settings.Encoding = Encoding.UTF8; settings.NewLineOnAttributes = true; XmlWriter sr = null; try { XmlSerializer xr = new XmlSerializer(typeof(T)); StringBuilder sb = new StringBuilder(); sr = XmlWriter.Create(sb, settings); xr.Serialize(sr, instance, ns); return (sb.ToString()); } catch (Exception ex) { //Logger.Log(String.Format("Serialization Fail. Error Message : {0} {1}{2}", ex.Message, Environment.NewLine, ex.StackTrace)); return null; } finally { if (sr != null) { sr.Close(); } } } /// <summary> /// XMLs to entity. /// </summary> /// <typeparam name="T">Xml對應的Entity type</typeparam> /// <param name="xml">The XML.</param> /// <returns>Xml對應的Entity</returns> public static T Deserialize<T>(string xml) where T : class { if (string.IsNullOrEmpty(xml)) { return null; } else { XmlSerializer serializer = new XmlSerializer(typeof(T)); byte[] byteArray = Encoding.UTF8.GetBytes(xml); MemoryStream stream = new MemoryStream(byteArray); return serializer.Deserialize(stream) as T; } } } }
2.C#验证类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace EC_System.FrameWork { public class ValidateHelper { private const string REG_DATE = @"^(?ni:(?=d)((?‘year‘((1[6-9])|([2-9]d))dd)(?‘sep‘[/.-])(?‘month‘0?[1-9]|1[012])2(?‘day‘((?<!(2((0?[2469])|11)2))31)|(?<!2(0?2)2)(29|30)|((?<=((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00)232)29)|((0?[1-9])|(1d)|(2[0-8])))(?:(?=x20d)x20|$))?((?<time>((0?[1-9]|1[012])(:[0-5]d){0,2}(x20[AP]M))|([01]d|2[0-3])(:[0-5]d){1,2}))?)$"; private const string REG_PHONE = @"^((0[0-9]{2,3}){0,1}([0-9]{7,8}))$"; private const string REG_EMAIL = @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$"; private const string REG_MOBILE = @"(^0{0,1}13[0-9]{9}$)"; private const string REG_IDCARD = @"^([0-9]{14}|[0-9]{17})(x|[0-9]){1}$"; private const string REG_TIME = @"^((([0-1]?[0-9])|(2[0-3]))([:])([0-5][0-9]))$"; private const string REG_Num = @"^[0-9]*$"; private const string REG_Decimal = @"^[0-9]+(.[0-9]{1,2})?$"; private const string REG_Num2 = @"^-?[0-9]d*$"; #region 数字有效性验证 /* 可以匹配如下格式 12:30 PM 2012-02-18 2012/2/18 02:31:35 2012-02-18 15:44:45 2012/2/12 02:31:35 AM */ /// <summary> /// 数字有效性验证 /// </summary> /// <param name="date">字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsNum(string num) { return Regex.IsMatch(num, REG_Num); } /// <summary> /// 正负整数 /// </summary> /// <param name="num"></param> /// <returns></returns> public static bool IsNum2(string num) { return Regex.IsMatch(num, REG_Num2); } public static bool IsDecimal(string num) { return Regex.IsMatch(num, REG_Decimal); } #endregion #region 日期字符串有效性验证 /// <summary> /// 日期字符串有效性验证 /// </summary> /// <param name="date">日期字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsValidDate(string date) { return Regex.IsMatch(date, REG_DATE); } #endregion #region Email有效性验证 /// <summary> /// Email有效性验证 /// </summary> /// <param name="email">Email字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsValidEmail(string email) { return Regex.IsMatch(email, REG_EMAIL); } #endregion #region 电话号码有效性验证 /// <summary> /// 电话号码有效性验证 /// </summary> /// <param name="phone">电话号码字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsVaildPhone(string phone) { return Regex.IsMatch(phone, REG_PHONE); } #endregion #region 手机号码有效性验证 /// <summary> /// 手机号码有效性验证 /// </summary> /// <param name="mobile">手机号码字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsValidMobile(string mobile) { return Regex.IsMatch(mobile, REG_MOBILE); } #endregion #region 身份证号有效性验证 /// <summary> /// 身份证号有效性验证 /// </summary> /// <param name="idCard">身份证号字符串</param> /// <returns>有效:true,否则:false</returns> public static bool IsValidIdCard(string idCard) { return Regex.IsMatch(idCard, REG_IDCARD); } #endregion } }
以上是关于基础类库--E-commerce的主要内容,如果未能解决你的问题,请参考以下文章
职位 | Blockchain Developer(E-Commerce)