title: Using xml for a configuration file
body: <p>I'm trying to figure out how to use <code>XmlTextReader</code> and <code>XmlTextWriter</code> for the configuration file of my program(s).</p>
  
  <p>The xml file looks like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;Base&gt;
      &lt;Global&gt;
          &lt;some_config_value&gt;86400&lt;/some_config_value&gt;
          &lt;test1&gt;t_1&lt;/test1&gt;
          &lt;test2&gt;t_2&lt;/test2&gt;
          &lt;test3&gt;t_3&lt;/test3&gt;
          &lt;test4&gt;t_4&lt;/test4&gt;
      &lt;/Global&gt;
      &lt;test_head&gt;
          &lt;test5&gt;t_5&lt;/test5&gt;
          &lt;test6&gt;t_6&lt;/test6&gt;
      &lt;/test_head&gt;
  &lt;/Base&gt;
  </code></pre>
  
  <p>And here is the class I have so far:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Xml;
  
  namespace my_program.Global
  {
      class CXMLConfig
      {
          private string path;
  
          public CXMLConfig(string filepath)
          {
              path = filepath;
          }
  
          public string XmlReadValue(string Section, string Key)
          {
              XmlTextReader textReader = new XmlTextReader(path);
              ReadElements readEl = new ReadElements(textReader, Section, Key);
              textReader.Close();
              return readEl.Value;
          }
  
          private class ReadElements
          {
              XmlTextReader textReader;
              string Section;
              string Key;
  
              private bool inBase = false;
              private bool inSection = false;
              private bool inKey = false;
  
              public string Value { get; private set; }
  
              public ReadElements(XmlTextReader textReader_set, string Section_set, string Key_set)
              {
                  Value = "";
  
                  this.textReader = textReader_set;
                  this.Section = Section_set;
                  this.Key = Key_set;
  
                  textReader.Read();
                  while (textReader.Read())
                  {
                      // Move to fist element
                      textReader.MoveToElement();
  
                      string nodetype = textReader.NodeType.ToString();
  
                      if (textReader.LocalName == "Base")
                      {
                          if (nodetype == "Element")
                          {
                              if (!inBase)
                                  inBase = true;
                          }
                          else if (nodetype == "EndElement")
                          {
                              if (inBase)
                                  inBase = false;
                          }
                      }
                      else if (inBase &amp;&amp; textReader.LocalName == Section)
                      {
                          if (nodetype == "Element")
                          {
                              if (!inSection)
                                  inSection = true;
                          }
                          else if (nodetype == "EndElement")
                          {
                              if (inSection)
                                  inSection = false;
                          }
                      }
                      else if (inBase &amp;&amp; inSection &amp;&amp; textReader.LocalName == Key)
                      {
                          if (inSection)
                          {
                              if (nodetype == "Element")
                              { 
                                  if (!inKey)
                                      inKey = true;
                              }
                              else if (nodetype == "EndElement")
                              {
                                  if (inKey)
                                      inKey = false;
                              }
                          }
                      }
                      else if (inBase &amp;&amp; inSection &amp;&amp; inKey)
                      {
                          if (nodetype == "Text")
                          {
                              Value = textReader.Value.ToString();
                              //Console.WriteLine(Value);
                          }
                      }
                  }
              }
  
          }
      }
  }
  </code></pre>
  
  <p>So first of all, this is probably bad XML.. I have never used it before and it does look a little.. odd. And then there is the fact that I wrote this whole <code>ReadElements</code> class to read a value out of the config file, but I imagined there would be a much simpler way to do this with <code>XmlTextReader</code> (but I couldn't find it). And then lastly, I have yet to figure out how to update a value in the xml file using <code>XmlTextWriter</code> without re-writing the whole xml file from top to bottom.</p>