123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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.Xml;
- using System.Xml.Linq;
- namespace MECF.Framework.Common.Config
- {
- public static class XmlSerializerExtension
- {
- public static T Read<T>(string fileName, out string reason) where T : class
- {
- try
- {
- reason = string.Empty;
- if (!File.Exists(fileName))
- {
- reason = $"{fileName}文件名不存在";
- return null;
- }
- using (StreamReader fs = new StreamReader(fileName))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- return (serializer.Deserialize(fs) as T);
- }
- }
- catch (Exception ex)
- {
- reason = $"Failed to XmlHelper.Read({fileName})\r\n{ex.Message}\r\n{ex.StackTrace}";
- return null;
- }
- }
- public static T Read<T>(string fileContent) where T : class
- {
- try
- {
- using (var reader = new StringReader(fileContent))
- {
- XmlSerializer serializer = new XmlSerializer(typeof(T));
- return (serializer.Deserialize(reader) as T);
- }
- }
- catch (Exception ex)
- {
- return null;
- }
- }
- }
- }
|