Reading and Writing XML Configuration Files
-
I've been reading through the projects here on codeproject, but i still don't fully grasp all of this. I'm writing a simple class that I can use manage application settings in my forms. Note, it is not meant to handle nested elements. There will be one root element "" and inside that will be the settings as key/value pairs. Here's what I've got so far: class Config { private string m_Path; private string m_Err; private XmlDocument xmlDoc; private XmlNode xmlNode; // The Config constructor - supply the path name to the file, load the document public Config(string Path) { m_Path = Path; Load(); } // Load the stored settings into the XmlDocument public bool Load() { try { XmlTextReader xtr = new XmlTextReader(m_Path); } catch (Exception ex) { m_Err = ex.ToString(); return false; } try { xmlDoc = new XmlDocument(); xmlDoc.Load(xtr); } catch (Exception ex) { m_Err = ex.ToString(); return false; } return true; } // Attempt to return the value, given the key name public string GetValue(string key) { // ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL"; return "NULL"; } // Attempt to set the value of the given key. If it does not exist, create a new key/value // Upon creation or modification of the XmlDocument, write the document to the configuration file public bool SetValue(string key, string value) { // ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE // IF NODE IS FOUND, MODIFY THE VALUE // IF NODE IS NOT FOUND, CREATE THE NEW VALUE return true; } // Return an arraylist containing all key/value pairs as such: "key:value" public ArrayList GetAllValues() { ArrayList aryList = new ArrayList(); // Finish Me! // foreach(key k in XmlDocument) // aryList.Add((string)(key) + (string)(value)); return aryList; } public bool WriteFile()
-
I've been reading through the projects here on codeproject, but i still don't fully grasp all of this. I'm writing a simple class that I can use manage application settings in my forms. Note, it is not meant to handle nested elements. There will be one root element "" and inside that will be the settings as key/value pairs. Here's what I've got so far: class Config { private string m_Path; private string m_Err; private XmlDocument xmlDoc; private XmlNode xmlNode; // The Config constructor - supply the path name to the file, load the document public Config(string Path) { m_Path = Path; Load(); } // Load the stored settings into the XmlDocument public bool Load() { try { XmlTextReader xtr = new XmlTextReader(m_Path); } catch (Exception ex) { m_Err = ex.ToString(); return false; } try { xmlDoc = new XmlDocument(); xmlDoc.Load(xtr); } catch (Exception ex) { m_Err = ex.ToString(); return false; } return true; } // Attempt to return the value, given the key name public string GetValue(string key) { // ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL"; return "NULL"; } // Attempt to set the value of the given key. If it does not exist, create a new key/value // Upon creation or modification of the XmlDocument, write the document to the configuration file public bool SetValue(string key, string value) { // ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE // IF NODE IS FOUND, MODIFY THE VALUE // IF NODE IS NOT FOUND, CREATE THE NEW VALUE return true; } // Return an arraylist containing all key/value pairs as such: "key:value" public ArrayList GetAllValues() { ArrayList aryList = new ArrayList(); // Finish Me! // foreach(key k in XmlDocument) // aryList.Add((string)(key) + (string)(value)); return aryList; } public bool WriteFile()
budidharma wrote:
public string GetValue(string key) { // ITERATE THROUGH KEYS, IF FOUND, RETURN THE VALUE, OTHERWISE, return "NULL"; return "NULL"; }
You can use the SelectNodes[^] or the SelectSingleNode[^] method on XmlDocument to do that. Give it an XPath expression and it will return you the appropriate nodes. In your case, it will probably look like
xmlDoc.SelectNodes("//nodeName[@key='keyValue'");
assuming the key value pairs are of the form <nodeName key="KeyValue value="Value>
budidharma wrote:
public bool SetValue(string key, string value) { // ITERATE THROUGH THE VALUES, FIND THE CORRECT NODE // IF NODE IS FOUND, MODIFY THE VALUE // IF NODE IS NOT FOUND, CREATE THE NEW VALUE return true; }
To modify an existing node, get a reference to it using the above method. Then use the Attributes property and the indexer to get a reference to the attribute and then just set the Value[^] property to the desired value. You can use the CreateNode[^] method to create a new node.
budidharma wrote:
public ArrayList GetAllValues() {
You can again use Sele