/// <summary> /// Loads the config values from a specific config element /// </summary> /// <param name="root">the root config element</param> protected virtual void LoadFromConfig(ConfigElement root) { string ip = root["Server"]["IP"].GetString("any"); if (ip == "any") _ip = IPAddress.Any; else _ip = IPAddress.Parse(ip); _port = (ushort)root["Server"]["Port"].GetInt(_port); }
/// <summary> /// Saves a single config element in an xml stream /// </summary> /// <param name="writer">the xml text writer</param> /// <param name="name">the name for this element</param> /// <param name="element">the element to save</param> protected void SaveElement(XmlTextWriter writer, string name, ConfigElement element) { bool badName = IsBadXMLElementName(name); if(element.HasChildren) { if(name == null) name = "root"; if(badName) { writer.WriteStartElement("param"); writer.WriteAttributeString("name",name); } else { writer.WriteStartElement(name); } foreach(DictionaryEntry entry in element.Children) { SaveElement(writer, (string) entry.Key, (ConfigElement) entry.Value); } writer.WriteEndElement(); } else { if(name != null) { if(badName) { writer.WriteStartElement("param"); writer.WriteAttributeString("name",name); writer.WriteString(element.GetString()); writer.WriteEndElement(); } else { writer.WriteElementString(name, element.GetString()); } } } }
/// <summary> /// Constructs a new XML config file element /// </summary> /// <param name="parent">The parent of the XML config file element</param> protected XMLConfigFile(ConfigElement parent): base(parent) { }
/// <summary> /// Loads the xml configuration from a file /// </summary> /// <param name="configFile">The config file</param> /// <returns>The parsed config</returns> public static XMLConfigFile ParseXMLFile(FileInfo configFile) { XMLConfigFile root = new XMLConfigFile(null); if(!configFile.Exists) return root; ConfigElement current = root; XmlTextReader reader = new XmlTextReader(configFile.OpenRead()); while(reader.Read()) { if(reader.NodeType == XmlNodeType.Element) { if(reader.Name=="root") continue; if(reader.Name=="param") { string name = reader.GetAttribute("name"); if(name != null && name != "root") { ConfigElement newElement = new ConfigElement(current); current[name] = newElement; current = newElement; } } else { ConfigElement newElement = new ConfigElement(current); current[reader.Name] = newElement; current = newElement; } } else if(reader.NodeType == XmlNodeType.Text) { current.Set(reader.Value); } else if(reader.NodeType == XmlNodeType.EndElement) { if(reader.Name!="root") current = current.Parent; } } reader.Close(); return root; }
/// <summary> /// Saves the values into a specific config element /// </summary> /// <param name="root">the root config element</param> protected virtual void SaveToConfig(ConfigElement root) { root["Server"]["Port"].Set(_port); root["Server"]["IP"].Set(_ip); }
/// <summary> /// Constructs a new config element with the given parent. /// </summary> /// <param name="parent">The parent element of the newly created element</param> public ConfigElement(ConfigElement parent) { m_parent = parent; }
/// <summary> /// Creates and returns a new configuration element. /// Can be used to create own configuration elements by /// overwriting this method /// </summary> /// <param name="parent">The parent element of the newly created element</param> /// <returns>The newly created config element</returns> protected virtual ConfigElement GetNewConfigElement(ConfigElement parent) { return new ConfigElement(parent); }
protected XMLConfigFile(ConfigElement parent) : base(parent) { }