首页 > 代码库 > .NET XmlHelper帮助类

.NET XmlHelper帮助类

  public class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student() { Name = "okbase", Age = 10 };
            string xml = XmlHelper.Serializer(typeof(Student), stu1);
            Console.Write(xml);
            Student stu2 = XmlHelper.Deserialize(typeof(Student), xml) as Student;
            Console.Write(string.Format("名字:{0},年龄:{1}", stu2.Name, stu2.Age));
            Console.ReadKey();
        }
        public class Student
        {
            public string Name { set; get; }
            public int Age { set; get; }
        }
        /// <summary>
        /// 将传入的字符串中间部分字符替换成特殊字符
        /// </summary>
        /// <param name="value">需要替换的字符串</param>
        /// <param name="startLen">前保留长度</param>
        /// <param name="endLen">尾保留长度</param>
        /// <param name="replaceChar">特殊字符</param>
        /// <returns>被特殊字符替换的字符串</returns>
        private static string ReplaceWithSpecialChar(string value, int startLen = 4, int endLen = 4, char specialChar = ‘*‘)
        {
            try
            {
                int lenth = value.Length - startLen - endLen;
                string replaceStr = value.Substring(startLen, lenth);
                string specialStr = string.Empty;
                for (int i = 0; i < replaceStr.Length; i++)
                {
                    specialStr += specialChar;
                }
                value = http://www.mamicode.com/value.Replace(replaceStr, specialStr);>

  使用方法

public class Program
    {
        static void Main(string[] args)
        {
            Student stu1 = new Student() { Name = "okbase", Age = 10 };
            string xml = XmlHelper.Serializer(typeof(Student), stu1);
            Console.Write(xml);
            Student stu2 = XmlHelper.Deserialize(typeof(Student), xml) as Student;
            Console.Write(string.Format("名字:{0},年龄:{1}", stu2.Name, stu2.Age));
            Console.ReadKey();
        }
        public class Student
        {
            public string Name { set; get; }
            public int Age { set; get; }
        }
    }

 

  

.NET XmlHelper帮助类