码迷,mamicode.com
首页 > Web开发 > 详细

.NET XmlHelper帮助类

时间:2017-07-09 10:30:33      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:eof   int end   XML   帮助   cat   ons   base   ret   ++   

  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 = value.Replace(replaceStr, specialStr);
            }
            catch (Exception)
            {
                throw;
            }
            return value;
        }
    }

  使用方法

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帮助类

标签:eof   int end   XML   帮助   cat   ons   base   ret   ++   

原文地址:http://www.cnblogs.com/qzxj/p/7140522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!