• XmlHelper


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml.Linq;
    using System.Xml.Serialization;
    
    namespace IOSerialize.Serialize
    {
        /// <summary>
        /// 使用序列化器完成的
        /// </summary>
        public class XmlHelper
        {
    
            /// <summary>
            /// 通过XmlSerializer序列化实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="t"></param>
            /// <returns></returns>
            public static string ToXml<T>(T t) where T : new()
            {
                XmlSerializer xmlSerializer = new XmlSerializer(t.GetType());
                Stream stream = new MemoryStream();
                xmlSerializer.Serialize(stream, t);
                stream.Position = 0;
                StreamReader reader = new StreamReader(stream);
                string text = reader.ReadToEnd();
                return text;
            }
    
            /// <summary>
            /// 字符串序列化成XML
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="content"></param>
            /// <returns></returns>
            public static T ToObject<T>(string content) where T : new()
            {
                using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
                    return (T)xmlFormat.Deserialize(stream);
                }
            }
    
            /// <summary>
            /// 文件反序列化成实体
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="fileName"></param>
            /// <returns></returns>
            public static T FileToObject<T>(string fileName) where T : new()
            {
                string CurrentXMLPath = ConfigurationManager.AppSettings["CurrentXMLPath"];
                fileName = Path.Combine(CurrentXMLPath, @"Student.xml");
                using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
                {
                    XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
                    return (T)xmlFormat.Deserialize(fStream);
                }
            }
        }
    }
  • 相关阅读:
    lintcode395-硬币排成线 II
    lintcode-394-硬币排成线
    lintcode-392-打劫房屋
    lintcode-391-数飞机
    lintcode-389-判断数独是否合法
    lintcode-387-最小差
    lintcode-384-最长无重复字符的子串
    lintcode-383-装最多水的容器
    lintcode-382-三角形计数
    爬虫笔记:初始爬虫(二)
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7726473.html
Copyright © 2020-2023  润新知