49

The following code should find the appropriate project tag and remove it from the XmlDocument, however when I test it, it says:

The node to be removed is not a child of this node.

Does anyone know the proper way to do this?

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 < projectNodes.Count; i++)
    {
        if (projectNodes[i].Attributes["name"] != null)
        {
            if (projectName == projectNodes[i].Attributes["name"].InnerText)
            {                                                
                configDoc.RemoveChild(projectNodes[i]);
                configDoc.Save(ccConfigPath);
            }
        }
    }
}

UPDATE

Fixed. I did two things:

XmlNode project = configDoc.SelectSingleNode("//project[@name='" + projectName + "']");

Replaced the For loop with an XPath query, which wasn't for fixing it, just because it was a better approach.

The actual fix was:

project.ParentNode.RemoveChild(project);

Thanks Pat and Chuck for this suggestion.

FlySwat
  • 172,459
  • 74
  • 246
  • 311
  • I was looking for just this thing. I spent a half a day looking for xml stuff on the internet and half a minute on SO. Yet another reason why Jeff and Joel were on to something. – Anthony Potts Dec 07 '09 at 19:29

6 Answers6

73

Instead of

configDoc.RemoveChild(projectNodes[i]);

try

projectNodes[i].parentNode.RemoveChild(projectNodes[i]);
Pat
  • 36,282
  • 18
  • 72
  • 87
2

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!

public static class Extensions {
    ...
    public static XmlNode RemoveFromParent(this XmlNode node) {
        return (node == null) ? null : node.ParentNode.RemoveChild(node);
    }
}
...
//some_long_node_expression.parentNode.RemoveChild(some_long_node_expression);
some_long_node_expression.RemoveFromParent();
Eugene Ryabtsev
  • 2,232
  • 1
  • 23
  • 37
2

try

configDoc.DocumentElement.RemoveChild(projectNodes[i]);
Jason
  • 86,222
  • 15
  • 131
  • 146
2

Looks like you need to select the parent node of projectNodes[i] before calling RemoveChild.

1

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.

Greg Hurlman
  • 17,666
  • 6
  • 54
  • 86
0

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

<Root>
 <Blah>
   <project>...</project>
 </Blah>
</Root>

The error message seems to be because you're trying to remove <project> from the grandparent rather than the direct parent of the project node

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
David Hayes
  • 7,402
  • 14
  • 50
  • 62