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

[{'answer_id': 20614, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 1, 'selected': False, 'text': "<p>Is it possible that the project nodes aren't child nodes, but grandchildren or lower?  GetElementsByTagName will give you elements from anywhere in the child element tree, IIRC.</p>\n"}, {'answer_id': 20624, 'author': 'David Hayes', 'author_id': 1769, 'author_profile': 'https://Stackoverflow.com/users/1769', 'pm_score': 0, 'selected': False, 'text': "<p>It would be handy to see a sample of the XML file you're processing but my guess would be that you have something like this</p>\n\n<pre><code>&lt;Root&gt;\n &lt;Blah&gt;\n   &lt;project&gt;...&lt;/project&gt;\n &lt;/Blah&gt;\n&lt;/Root&gt;\n</code></pre>\n\n<p>The error message seems to be because you're trying to remove <code>&lt;project&gt;</code> from the grandparent rather than the direct parent of the project node</p>\n"}, {'answer_id': 20625, 'author': 'Pat', 'author_id': 238, 'author_profile': 'https://Stackoverflow.com/users/238', 'pm_score': 7, 'selected': True, 'text': '<p>Instead of </p>\n\n<pre><code>configDoc.RemoveChild(projectNodes[i]);\n</code></pre>\n\n<p>try </p>\n\n<pre><code>projectNodes[i].parentNode.RemoveChild(projectNodes[i]);\n</code></pre>\n'}, {'answer_id': 20629, 'author': 'Jason', 'author_id': 1338, 'author_profile': 'https://Stackoverflow.com/users/1338', 'pm_score': 2, 'selected': False, 'text': '<p>try </p>\n\n<pre><code>configDoc.DocumentElement.RemoveChild(projectNodes[i]);\n</code></pre>\n'}, {'answer_id': 20636, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Looks like you need to select the parent node of projectNodes[i] before calling RemoveChild.</p>\n'}, {'answer_id': 11116820, 'author': 'Eugene Ryabtsev', 'author_id': 1353187, 'author_profile': 'https://Stackoverflow.com/users/1353187', 'pm_score': 1, 'selected': False, 'text': '<p>When you get sufficiently annoyed by writing it the long way (for me that was fairly soon) you can use a helper extension method provided below. Yay new technology!</p>\n\n<pre><code>public static class Extensions {\n    ...\n    public static XmlNode RemoveFromParent(this XmlNode node) {\n        return (node == null) ? null : node.ParentNode.RemoveChild(node);\n    }\n}\n...\n//some_long_node_expression.parentNode.RemoveChild(some_long_node_expression);\nsome_long_node_expression.RemoveFromParent();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/20611', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1965/']