title: Removing nodes from an XmlDocument
body: <p>The following code should find the appropriate project tag and remove it from the XmlDocument, however when I test it, it says:</p>
  
  <p><strong>The node to be removed is not a child of this node.</strong></p>
  
  <p>Does anyone know the proper way to do this?</p>
  
  <pre><code>public void DeleteProject (string projectName)
  {
      string ccConfigPath = ConfigurationManager.AppSettings["ConfigPath"];
  
      XmlDocument configDoc = new XmlDocument();
  
      configDoc.Load(ccConfigPath);
  
      XmlNodeList projectNodes = configDoc.GetElementsByTagName("project");
  
      for (int i = 0; i &lt; projectNodes.Count; i++)
      {
          if (projectNodes[i].Attributes["name"] != null)
          {
              if (projectName == projectNodes[i].Attributes["name"].InnerText)
              {                                                
                  configDoc.RemoveChild(projectNodes[i]);
                  configDoc.Save(ccConfigPath);
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Fixed. I did two things:</p>
  
  <pre><code>XmlNode project = configDoc.SelectSingleNode("//project[@name='" + projectName + "']");
  </code></pre>
  
  <p>Replaced the For loop with an XPath query, which wasn't for fixing it, just because it was a better approach.</p>
  
  <p>The actual fix was:</p>
  
  <pre><code>project.ParentNode.RemoveChild(project);
  </code></pre>
  
  <p>Thanks Pat and Chuck for this suggestion.</p>