code_junkie

Removing nodes from an XmlDocument

Nov 10th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. public void DeleteProject (string projectName)
  2. {
  3. string ccConfigPath = ConfigurationManager.AppSettings["ConfigPath"];
  4.  
  5. XmlDocument configDoc = new XmlDocument();
  6.  
  7. configDoc.Load(ccConfigPath);
  8.  
  9. XmlNodeList projectNodes = configDoc.GetElementsByTagName("project");
  10.  
  11. for (int i = 0; i < projectNodes.Count; i++)
  12. {
  13. if (projectNodes[i].Attributes["name"] != null)
  14. {
  15. if (projectName == projectNodes[i].Attributes["name"].InnerText)
  16. {
  17. configDoc.RemoveChild(projectNodes[i]);
  18. configDoc.Save(ccConfigPath);
  19. }
  20. }
  21. }
  22. }
  23.  
  24. XmlNode project = configDoc.SelectSingleNode("//project[@name='" + projectName + "']");
  25.  
  26. project.ParentNode.RemoveChild(project);
  27.  
  28. configDoc.RemoveChild(projectNodes[i]);
  29.  
  30. projectNodes[i].parentNode.RemoveChild(projectNodes[i]);
  31.  
  32. <Root>
  33. <Blah>
  34. <project>...</project>
  35. </Blah>
  36. </Root>
  37.  
  38. configDoc.DocumentElement.RemoveChild(projectNodes[i]);
Add Comment
Please, Sign In to add comment