private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if (string.IsNullOrEmpty(strFilePath)) { Debug.Assert(false); return(null); } AppConfigEx tConfig = null; try { if (!File.Exists(strFilePath)) { return(null); } XmlSerializerEx xs = new XmlSerializerEx(typeof(AppConfigEx)); if (xdEnforced == null) { using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { tConfig = (AppConfigEx)xs.Deserialize(fs); } } else // Enforced configuration { XmlDocument xd = XmlUtilEx.CreateXmlDocument(); xd.Load(strFilePath); XmContext ctx = new XmContext(xd, AppConfigEx.GetNodeOptions, AppConfigEx.GetNodeKey); XmlUtil.MergeElements(xd.DocumentElement, xdEnforced.DocumentElement, "/" + xd.DocumentElement.Name, null, ctx); using (MemoryStream msW = new MemoryStream()) { xd.Save(msW); using (MemoryStream msR = new MemoryStream(msW.ToArray(), false)) { tConfig = (AppConfigEx)xs.Deserialize(msR); } } } } catch (Exception ex) { FileDialogsEx.ShowConfigError(strFilePath, ex.Message, false, true); } if (tConfig != null) { tConfig.OnLoad(); } return(tConfig); }
public static bool IsOptionEnforced(object pContainer, PropertyInfo pi) { if (pContainer == null) { Debug.Assert(false); return(false); } if (pi == null) { Debug.Assert(false); return(false); } XmlDocument xdEnforced = AppConfigSerializer.EnforcedConfigXml; if (xdEnforced == null) { return(false); } string strObjPath; if (!m_dictXmlPathCache.TryGetValue(pContainer, out strObjPath)) { strObjPath = XmlUtil.GetObjectXmlPath(Program.Config, pContainer); if (string.IsNullOrEmpty(strObjPath)) { Debug.Assert(false); return(false); } m_dictXmlPathCache[pContainer] = strObjPath; } string strProp = XmlSerializerEx.GetXmlName(pi); if (string.IsNullOrEmpty(strProp)) { Debug.Assert(false); return(false); } string strPre = strObjPath; if (!strPre.EndsWith("/")) { strPre += "/"; } string strXPath = strPre + strProp; XmlNode xn = xdEnforced.SelectSingleNode(strXPath); if (xn == null) { return(false); } XmContext ctx = new XmContext(null, AppConfigEx.GetNodeOptions, AppConfigEx.GetNodeKey); return(XmlUtil.IsAlwaysEnforced(xn, strXPath, ctx)); }
private static AppConfigEx LoadConfigFileEx(string strFilePath, XmlDocument xdEnforced) { if (string.IsNullOrEmpty(strFilePath)) { return(null); } AppConfigEx tConfig = null; XmlSerializerEx xmlSerial = new XmlSerializerEx(typeof(AppConfigEx)); if (xdEnforced == null) { try { using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { tConfig = (AppConfigEx)xmlSerial.Deserialize(fs); } } catch (Exception) { } // Do not assert } else // Enforced configuration { try { XmlDocument xd = XmlUtilEx.CreateXmlDocument(); xd.Load(strFilePath); XmContext ctx = new XmContext(xd, AppConfigEx.GetNodeOptions, AppConfigEx.GetNodeKey); XmlUtil.MergeElements(xd.DocumentElement, xdEnforced.DocumentElement, "/" + xd.DocumentElement.Name, null, ctx); using (MemoryStream msAsm = new MemoryStream()) { xd.Save(msAsm); using (MemoryStream msRead = new MemoryStream( msAsm.ToArray(), false)) { tConfig = (AppConfigEx)xmlSerial.Deserialize(msRead); } } } catch (FileNotFoundException) { } catch (Exception) { Debug.Assert(false); } } if (tConfig != null) { tConfig.OnLoad(); } return(tConfig); }