title: Removing all child node elements from XML in C#?
body: <p>I have the following XML structure:</p>
  <h1>XML</h1>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;Users&gt;
  &lt;/Users&gt;
  </code></pre>
  <p>At some point, the <code>&lt;Users&gt;</code> gets filled up with different users, but I want to be able to delete them all in a single, simple function.</p>
  <h1>Attempt</h1>
  <pre><code>/// &lt;summary&gt;
  /// Removes all nodes from XML creds file on application close
  /// &lt;/summary&gt;
  public static void RemoveXMLData()
  {
      string xmlPath = Path.Combine(Environment.CurrentDirectory, @&quot;Data\Credential.xml&quot;);
      XDocument document = XDocument.Load(xmlPath);
      document.RemoveNodes();
  }
  </code></pre>
  <p>This doesn't seem to be working. I've done the following as well:</p>
  <h1>Attempt #2</h1>
  <pre><code>        string xmlPath = Path.Combine(Environment.CurrentDirectory, @&quot;Data\Credential.xml&quot;);
          XDocument document = XDocument.Load(xmlPath);
  
          foreach (var node in document.Descendants(&quot;Users&quot;))
          {
              node.Remove();
          }
  </code></pre>
  <p>But this yields an exception error.</p>
  <p>At the end of the day, I just want to get back to square one with the following file data:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?&gt;
  &lt;Users&gt;
  &lt;/Users&gt;
  </code></pre>