public static XmlNode AddNodes(this XmlNode node, IEnumerable <string> nodeValues, string nodeName) { if (nodeValues != null && node != null && !string.IsNullOrEmpty(nodeName)) { nodeValues.ToList().ForEach(x => XMLUtil.AddNode(node, nodeName).SetValue(x)); } return(node); }
public void SetDefaultSettingValue(string name, string value) { var valuePath = getValuePath(name); if (XMLUtil.NodeExists(defaultroot, valuePath)) { XMLUtil.SetNodeValue(defaultroot, valuePath, value); } else { var cfg = XMLUtil.AddNode(defaultroot, "config"); XMLUtil.AddAttribute(cfg, "name", name); XMLUtil.AddAttribute(cfg, "value", value); } }
public void SetValue(string name, int value) { var valuePath = getValuePath(name); if (XMLUtil.NodeExists(root, valuePath)) { XMLUtil.SetNodeValue(root, valuePath, value.ToString()); } else { if (root != null) { var cfg = XMLUtil.AddNode(root, "config"); XMLUtil.AddAttribute(cfg, "name", name); XMLUtil.AddAttribute(cfg, "value", value.ToString()); } } }
public static XmlDocument ToXmlDocument(this string str, string rootName = "") { if (!str.IsEmpty()) { if (!string.IsNullOrEmpty(rootName)) { var doc = new XmlDocument(); var root = XMLUtil.AddNode(doc.DocumentElement, rootName); root.SetValue(str); return(doc); } else { return(XMLUtil.LoadXml(str)); } } return(null); }
public void LoadSettings() { if (xml == null) { xml = new XmlDocument(); } if (defaultxml == null) { defaultxml = new XmlDocument(); if (File.Exists(DefaultSettingFilePath)) { try { defaultxml.Load(DefaultSettingFilePath); defaultroot = XMLUtil.GetNode(defaultxml, "root"); } catch { defaultroot = null; } if (defaultroot == null) { defaultxml = new XmlDocument(); defaultroot = XMLUtil.AddNode(defaultxml, "root"); } } else { defaultroot = XMLUtil.AddNode(defaultxml, "root"); } } try { if (SettingFilePath == DefaultSettingFilePath) { xml = defaultxml; root = defaultroot; if (loadSaved) { var lastFile = GetDefaultSettingValue("lastSettingFile"); if (!string.IsNullOrEmpty(lastFile) && File.Exists(lastFile) && lastFile != DefaultSettingFilePath) { SettingFilePath = lastFile; if (File.Exists(SettingFilePath)) { try { XmlDocument doc = new XmlDocument(); doc.Load(SettingFilePath); this.xml = doc; root = XMLUtil.GetNode(xml, "root"); if (root == null) { xml = null; } } catch { } } } loadSaved = false; } } else { if (root == null) { if (File.Exists(SettingFilePath)) { try { XmlDocument x = new XmlDocument(); x.Load(SettingFilePath); this.xml = x; root = XMLUtil.GetNode(xml, "root"); if (root == null) { xml = null; } } catch { } } } } } catch { xml = defaultxml; root = defaultroot; } }
public static XmlNode AddNode(this XmlNode node, string name) { return(XMLUtil.AddNode(node, name)); }