using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(@"<Parts>
<Part name=""DisappearsOk"" disabled=""true""></Part>
<Part name=""KeepMe"" disabled=""false""></Part>
<Part name=""KeepMe2"" ></Part>
<Part name=""ShouldBeGone"" disabled=""true""></Part>
</Parts>");
XmlNode root = xmlDoc.DocumentElement;
List<XmlNode> disabledNodes = new List<XmlNode>();
try
{
foreach (XmlNode node in root.ChildNodes.Cast<XmlNode>()
.Where(child => child.Attributes["disabled"] != null &&
Convert.ToBoolean(child.Attributes["disabled"].Value)))
{
Console.WriteLine("Removing:");
Console.WriteLine(node.OuterXml);
root.RemoveChild(node);
}
}
catch (Exception Ex)
{
Console.WriteLine("Exception, as expected");
}
Console.WriteLine();
Console.WriteLine(root.OuterXml);
Console.ReadKey();
}
}
}
ICAgIHVzaW5nIFN5c3RlbTsKICAgIHVzaW5nIFN5c3RlbS5Db2xsZWN0aW9ucy5HZW5lcmljOwogICAgdXNpbmcgU3lzdGVtLkxpbnE7CiAgICB1c2luZyBTeXN0ZW0uWG1sOwogICAgCiAgICBuYW1lc3BhY2UgQ29uc29sZUFwcGxpY2F0aW9uMQogICAgewogICAgICAgIGNsYXNzIFByb2dyYW0KICAgICAgICB7CiAgICAgICAgICAgIHN0YXRpYyB2b2lkIE1haW4oc3RyaW5nW10gYXJncykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgWG1sRG9jdW1lbnQgeG1sRG9jID0gbmV3IFhtbERvY3VtZW50KCk7CiAgICAKICAgICAgICAgICAgICAgIHhtbERvYy5Mb2FkWG1sKEAiPFBhcnRzPgogICAgICA8UGFydCBuYW1lPSIiRGlzYXBwZWFyc09rIiIgZGlzYWJsZWQ9IiJ0cnVlIiI+PC9QYXJ0PgogICAgICA8UGFydCBuYW1lPSIiS2VlcE1lIiIgZGlzYWJsZWQ9IiJmYWxzZSIiPjwvUGFydD4KICAgICAgPFBhcnQgbmFtZT0iIktlZXBNZTIiIiA+PC9QYXJ0PgogICAgICA8UGFydCBuYW1lPSIiU2hvdWxkQmVHb25lIiIgZGlzYWJsZWQ9IiJ0cnVlIiI+PC9QYXJ0PiAgCiAgICA8L1BhcnRzPiIpOwogICAgCiAgICAgICAgICAgICAgICBYbWxOb2RlIHJvb3QgPSB4bWxEb2MuRG9jdW1lbnRFbGVtZW50OwogICAgICAgICAgICAgICAgTGlzdDxYbWxOb2RlPiBkaXNhYmxlZE5vZGVzID0gbmV3IExpc3Q8WG1sTm9kZT4oKTsKICAgIAogICAgICAgICAgICAgICAgdHJ5CiAgICAgICAgICAgICAgICB7CiAgICAKICAgICAgICAgICAgICAgICAgICBmb3JlYWNoIChYbWxOb2RlIG5vZGUgaW4gcm9vdC5DaGlsZE5vZGVzLkNhc3Q8WG1sTm9kZT4oKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgLldoZXJlKGNoaWxkID0+IGNoaWxkLkF0dHJpYnV0ZXNbImRpc2FibGVkIl0gIT0gbnVsbCAmJgogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIENvbnZlcnQuVG9Cb29sZWFuKGNoaWxkLkF0dHJpYnV0ZXNbImRpc2FibGVkIl0uVmFsdWUpKSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIENvbnNvbGUuV3JpdGVMaW5lKCJSZW1vdmluZzoiKTsKICAgICAgICAgICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUobm9kZS5PdXRlclhtbCk7CiAgICAgICAgICAgICAgICAgICAgICAgIHJvb3QuUmVtb3ZlQ2hpbGQobm9kZSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgY2F0Y2ggKEV4Y2VwdGlvbiBFeCkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgiRXhjZXB0aW9uLCBhcyBleHBlY3RlZCIpOwogICAgICAgICAgICAgICAgfQogICAgCiAgICAgICAgICAgICAgICBDb25zb2xlLldyaXRlTGluZSgpOwogICAgICAgICAgICAgICAgQ29uc29sZS5Xcml0ZUxpbmUocm9vdC5PdXRlclhtbCk7CiAgICAKICAgICAgICAgICAgICAgIENvbnNvbGUuUmVhZEtleSgpOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQ==