fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. XmlDocument xmlDoc = new XmlDocument();
  13.  
  14. xmlDoc.LoadXml(@"<Parts>
  15. <Part name=""DisappearsOk"" disabled=""true""></Part>
  16. <Part name=""KeepMe"" disabled=""false""></Part>
  17. <Part name=""KeepMe2"" ></Part>
  18. <Part name=""ShouldBeGone"" disabled=""true""></Part>
  19. </Parts>");
  20.  
  21. XmlNode root = xmlDoc.DocumentElement;
  22. List<XmlNode> disabledNodes = new List<XmlNode>();
  23.  
  24. try
  25. {
  26.  
  27. foreach (XmlNode node in root.ChildNodes.Cast<XmlNode>()
  28. .Where(child => child.Attributes["disabled"] != null &&
  29. Convert.ToBoolean(child.Attributes["disabled"].Value)))
  30. {
  31. Console.WriteLine("Removing:");
  32. Console.WriteLine(node.OuterXml);
  33. root.RemoveChild(node);
  34. }
  35. }
  36. catch (Exception Ex)
  37. {
  38. Console.WriteLine("Exception, as expected");
  39. }
  40.  
  41. Console.WriteLine();
  42. Console.WriteLine(root.OuterXml);
  43.  
  44. Console.ReadKey();
  45. }
  46. }
  47. }
Success #stdin #stdout 0.09s 34464KB
stdin
Standard input is empty
stdout
Removing:
<Part name="DisappearsOk" disabled="true"></Part>
Exception, as expected

<Parts><Part name="KeepMe" disabled="false"></Part><Part name="KeepMe2"></Part><Part name="ShouldBeGone" disabled="true"></Part></Parts>