From aaca034ba8967d383ba5192dd72ba34ccf1d703f Mon Sep 17 00:00:00 2001 From: sheychen Date: Tue, 13 Dec 2016 16:12:14 +0100 Subject: [PATCH] Add XmlManager --- MyCommon/MyCommon.csproj | 1 + MyCommon/Properties/AssemblyInfo.cs | 4 +- MyCommon/XmlManager.cs | 122 ++++++++++++++++++++++++++++ Readme.md | 1 + 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 MyCommon/XmlManager.cs diff --git a/MyCommon/MyCommon.csproj b/MyCommon/MyCommon.csproj index 3aa25e8..5524b0c 100644 --- a/MyCommon/MyCommon.csproj +++ b/MyCommon/MyCommon.csproj @@ -47,6 +47,7 @@ + diff --git a/MyCommon/Properties/AssemblyInfo.cs b/MyCommon/Properties/AssemblyInfo.cs index d52f5d9..84f7614 100644 --- a/MyCommon/Properties/AssemblyInfo.cs +++ b/MyCommon/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ using System.Runtime.InteropServices; // Vous pouvez spécifier toutes les valeurs ou indiquer les numéros de build et de révision par défaut // en utilisant '*', comme indiqué ci-dessous : // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.1.0.0")] -[assembly: AssemblyFileVersion("1.1.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.2.0.0")] +[assembly: AssemblyFileVersion("1.2.0.0")] \ No newline at end of file diff --git a/MyCommon/XmlManager.cs b/MyCommon/XmlManager.cs new file mode 100644 index 0000000..eb93de7 --- /dev/null +++ b/MyCommon/XmlManager.cs @@ -0,0 +1,122 @@ +using System.IO; +using System.Xml; +using System.Xml.Serialization; + +namespace MyCommon +{ + public static class XmlManager + { + public enum LoadMode { ReadOnly, ReadOrCreate, ReadCreateOrReplace }; + /// + /// Load class from xml file + /// + /// Path to xml file + /// null disable Correct check (unsafe) + /// Loaded class + /// App.config is too easy + public static T Load(string filepath, LoadMode mode, XmlReader schema, Logger logger = null) where T : new() + { + T file = new T(); + if (logger != null) { logger.Write("Loading " + file.GetType().Name, Logger.logType.info); } + if (File.Exists(filepath)) + { + bool correct = false; + if(schema != null) + { + correct = Correct(file, filepath, schema, logger); + } + else + { + correct = true; + } + if (correct) + { + XmlSerializer xs = new XmlSerializer(typeof(T)); + using (StreamReader re = new StreamReader(filepath)) + { + file = (T)xs.Deserialize(re); + }; + } + else + { + if (mode == LoadMode.ReadCreateOrReplace) + { + if (logger != null) { logger.Write("Old " + file.GetType().Name + " in .old", Logger.logType.warm); } + File.Delete(filepath + ".old"); + File.Move(filepath, filepath + ".old"); + Save(file, filepath, logger); + } + } + } + else + { + if (logger != null) { logger.Write("Any config file", Logger.logType.error); } + if (mode != LoadMode.ReadOnly) + { + Save(file, filepath, logger); + } + } + if (logger != null) { logger.Write(file.GetType().Name + " loaded", Logger.logType.debug); } + return file; + } + + /// + /// Write class in xml file + /// + public static void Save(T file, string path, Logger logger = null) + { + XmlSerializer xs = new XmlSerializer(typeof(T)); + using (StreamWriter st = new StreamWriter(path)) + { + xs.Serialize(st, file); + }; + if (logger != null) { logger.Write(file.GetType().Name + " saved", Logger.logType.debug); } + } + + /// + /// Check file format using Schema + /// + public static bool Correct(T file, string filepath, XmlReader schema, Logger logger = null) + { + bool isCorrect = false; + + using (Stream fs = new FileStream(filepath, FileMode.Open)) + { + XmlReader re = new XmlTextReader(fs); + XmlSerializer xs = new XmlSerializer(typeof(T)); + try + { + isCorrect = xs.CanDeserialize(re); + } + catch (XmlException e) + { + isCorrect = false; + if (logger != null) { logger.Write("Format check error: " + e.Message, Logger.logType.error); } + } + } + + if (isCorrect) + { + try + { + XmlDocument d = new XmlDocument(); + d.Load(filepath); + d.Schemas.Add("", schema); + + d.Validate((o, e) => + { + if (logger != null) { logger.Write("Format check error: " + e.Message, Logger.logType.error); } + isCorrect = false; + }); + } + catch (XmlException e) + { + isCorrect = false; + if (logger != null) { logger.Write("Format check error: " + e.Message, Logger.logType.error); } + } + } + + return isCorrect; + } + } +} diff --git a/Readme.md b/Readme.md index 536c685..402ec7d 100644 --- a/Readme.md +++ b/Readme.md @@ -6,6 +6,7 @@ MyCommon is a collection of usefull Classes * Binary * Logger * Strings +* XmlMananger - 13/12/2016 ### Prerequisities