title: How to delete node from XML file using C#
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/875136/how-to-remove-an-xmlnode-from-xmlnodelist">How to remove an XmlNode from XmlNodeList</a>  </p>
  </blockquote>
  
  <p>Hi, How can i delete a set of nodes from an XML file.? 
  Here is a code snippet.</p>
  
  <pre><code>string path = @"C:\Documents and Settings\e454935\Desktop\NUnitSettings.xml";
  FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
  System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
  xmldoc.Load(fs);
  fs.Close();
  xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
  FileStream WRITER = new FileStream(path, FileMode.Truncate, FileAccess.Write, FileShare.ReadWrite);
  xmldoc.Save(WRITER);
  WRITER.Close(); 
  </code></pre>
  
  <p>I tried the following code simply to delete a node and got
  "Object reference not set to an instance of an object." at </p>
  
  <pre><code>xmldoc.DocumentElement.RemoveChild(xmldoc.DocumentElement.ChildNodes[1]);
  </code></pre>
  
  <p>Here is a sample XML file,</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Xml1&gt;
    &lt;Settings&gt;
      &lt;Setting name="DisplayFormat" value="Full" /&gt;
      &lt;Setting name="File1" value="a" /&gt;
      &lt;Setting name="File1" value="b" /&gt;
      &lt;Setting name="File1" value="c" /&gt;
      &lt;Setting name="File1" value="d" /&gt;
    &lt;/Settings&gt;
  &lt;/Xml1&gt;
  </code></pre>
  
  <p>Actually from this file i want to delete the <strong>Four File1 nodes</strong> which has the values "a,b,c,d" and then i want to <strong>add a node</strong>,</p>
  
  <pre><code>&lt;Setting name="File1" value="e" /&gt;
  </code></pre>
  
  <p>How can i do this.?</p>