123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Threading.Tasks;
- using System.Xml;
- using System.Xml.Linq;
- using System.Xml.Serialization;
- namespace XmlClass
- {
- public class ModelXmlReadWrite
- {
- private static readonly ModelXmlReadWrite modelXmlReadWrite = new ModelXmlReadWrite();
- private string XmlEncryptionString = "jiuzhankejijiami";
- string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Config.xml";
- private ModelXmlReadWrite() { }
- public static ModelXmlReadWrite GetModelXmlReadWrite()
- {
- return modelXmlReadWrite;
- }
-
- /// <summary>
- /// 封装一个继承StringWrite的方法(可定义Encoding)
- /// </summary>
- public sealed class StringWriterWithEncoding : StringWriter
- {
- private readonly Encoding encoding;
- public StringWriterWithEncoding(Encoding encoding)
- {
- this.encoding = encoding;
- }
- public override Encoding Encoding
- {
- get
- {
- return encoding;
- }
- }
- }
- /// <summary>
- /// 写入Xml
- /// 使用方法 WriteXml(Model)
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public string WriteXml(object obj)
- {
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- string content = string.Empty;
- //序列化
- using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
- {
- serializer.Serialize(writer, obj);
- content = writer.ToString();
- }
- //保存文件
- using (StreamWriter stream_writer = new StreamWriter(path, false, Encoding.UTF8))
- {
- stream_writer.Write(content);
- }
- return content;
- }
- /// <summary>
- /// 写入Xml,带路径
- /// 使用方法 WriteXml(Model)
- /// </summary>
- /// <param name="obj"></param>
- /// <returns></returns>
- public string WriteXml(object obj,string WXPath)
- {
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- string content = string.Empty;
- //序列化
- using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
- {
- serializer.Serialize(writer, obj);
- content = writer.ToString();
- }
- //保存文件
- using (StreamWriter stream_writer = new StreamWriter(WXPath, false, Encoding.UTF8))
- {
- stream_writer.Write(content);
- }
- return content;
- }
- /// <summary>
- /// 写入Xml(带加密)
- /// </summary>
- /// <param name="obj">Model值</param>
- /// <param name="IsEncryption">是否加密</param>
- /// <returns></returns>
- public string WriteXml(object obj,bool IsEncryption)
- {
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- string content = string.Empty;
- //序列化
- using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
- {
- serializer.Serialize(writer, obj);
- content = writer.ToString();
- }
- //保存文件
- using (StreamWriter stream_writer = new StreamWriter(path, false, Encoding.UTF8))
- {
- stream_writer.Write(content);
- }
- if (IsEncryption)
- {
- XmlEncryption(path);
- }
- return content;
- }
- /// <summary>
- /// 读取Xml
- /// 使用方法 Model=(Model)ReadXml(typeof(Model))
- /// </summary>
- /// <param name="object_type">typeof(Model)</param>
- /// <returns></returns>
- public object ReadXml(Type object_type,string XRPath)
- {
- XmlSerializer serializer = new XmlSerializer(object_type);
- using (StreamReader reader = new StreamReader(XRPath, Encoding.UTF8))
- {
- //反序列化
- return serializer.Deserialize(reader);
- }
- }
- /// <summary>
- /// 读取Xml
- /// 使用方法 Model=(Model)ReadXml(typeof(Model))
- /// </summary>
- /// <param name="object_type">typeof(Model)</param>
- /// <returns></returns>
- public object ReadXml(Type object_type)
- {
- XmlSerializer serializer = new XmlSerializer(object_type);
- using (StreamReader reader = new StreamReader(path,Encoding.UTF8))
- {
- //反序列化
- return serializer.Deserialize(reader);
- }
- }
- /// <summary>
- /// 读取Xml
- /// 使用方法 Model=(Model)ReadXml(typeof(Model))
- /// </summary>
- /// <param name="object_type"></param>
- /// <param name="IsEncryption"></param>
- /// <returns></returns>
- public object ReadXml(Type object_type, bool IsEncryption)
- {
- object result = null;
- if (IsEncryption)
- {
- XmlDecrypt(path);
- }
- XmlSerializer serializer = new XmlSerializer(object_type);
- using (StreamReader reader = new StreamReader(path, Encoding.UTF8))
- {
- result = serializer.Deserialize(reader);
- //反序列化
- }
- if (IsEncryption)
- {
- XmlEncryption(path);
- }
- return result;
- }
- public bool CheckConfigDafaultPath()
- {
- bool result = false;
- try
- {
- if(File.Exists(path))
- {
- result = true;
- }
- }
- catch
- {
- }
- return result;
- }
- /// <summary>
- /// Xml秘钥加密
- /// </summary>
- /// <param name="XmlPath"></param>
- private void XmlEncryption(string XmlPath)
- {
- string XmlText = "";
- XmlDocument xmlDoc = new XmlDocument();
- XmlDocument result = new XmlDocument();
- xmlDoc.PreserveWhitespace = true;
- xmlDoc.Load(XmlPath);//加载要加密的XML文件
- XmlText = xmlDoc.InnerXml;
- if (string.IsNullOrEmpty(XmlText)) return;
- try
- {
- Byte[] toEncryptArray = Encoding.UTF8.GetBytes(XmlText);
- RijndaelManaged rm = new RijndaelManaged
- {
- //设置密钥:key为32位=数字或字母16个=汉字8个
- Key = Encoding.UTF8.GetBytes(XmlEncryptionString),
- Mode = CipherMode.ECB,
- Padding = PaddingMode.PKCS7
- };
- ICryptoTransform cTransform = rm.CreateEncryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- string MessageValue = Convert.ToBase64String(resultArray, 0, resultArray.Length);
- //创建类型声明节点
- XmlNode node = result.CreateXmlDeclaration("1.0", "utf-8", "");
- result.AppendChild(node);
- //创建根节点
- XmlNode root = result.CreateElement("XmlText");
- result.AppendChild(root);
- XmlNode Modeltext = result.CreateElement("Model");
- root.AppendChild(Modeltext);
- CreateNode(result, Modeltext, "value", MessageValue);
- }
- catch
- {
- //MessageBox.Show("配置文件异常");
- }
- result.Save(XmlPath);//生成加密后的XML文件
- }
- /// <summary>
- /// 秘钥解密
- /// </summary>
- /// <param name="XmlPath"></param>
- private void XmlDecrypt(string XmlPath)
- {
- XElement result = XElement.Parse("<value></value>");
- string XmlTextValue = string.Empty;
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.PreserveWhitespace = true;
- XElement xe = XElement.Load(XmlPath);
- IEnumerable<XElement> elements = from ele in xe.Elements("Model") select ele;
- //读取Xml文件并解密
- foreach (var ele in elements)
- {
- try
- {
- XmlTextValue = Convert.ToString(ele.Element("value").Value);
- //xe = XmlDecrypt(XmlTextValue);
- }
- catch
- {
- //MessageBox.Show("配置文件异常");
- return;
- }
- }
- //分析解密后的数据
- if (string.IsNullOrEmpty(XmlTextValue)) return;
- try
- {
- Byte[] toEncryptArray = Convert.FromBase64String(XmlTextValue);
- RijndaelManaged rm = new RijndaelManaged
- {
- Key = Encoding.UTF8.GetBytes(XmlEncryptionString),
- Mode = CipherMode.ECB,
- Padding = PaddingMode.PKCS7
- };
- ICryptoTransform cTransform = rm.CreateDecryptor();
- Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
- XmlTextValue = Encoding.UTF8.GetString(resultArray);
- result = XElement.Parse(XmlTextValue);
- result.Save(path);
- }
- catch
- {
- //MessageBox.Show("配置文件异常");
- }
- }
- /// <summary>
- /// 创建节点
- /// </summary>
- /// <param name="xmldoc"></param> xml文档
- /// <param name="parentnode"></param>父节点
- /// <param name="name"></param> 节点名
- /// <param name="value"></param> 节点值
- private void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
- {
- XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
- node.InnerText = value;
- parentNode.AppendChild(node);
- }
- }
- }
|