Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading and Writing XML Configuration Files

Reading and Writing XML Configuration Files

Scheduled Pinned Locked Moved C#
xmlworkspace
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    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()

    S 1 Reply Last reply
    0
    • B budidharma

      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()

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups