Xdocument Xd (mapped from xml file):
<pfsense>
<nat>
<outbound>
<mode>advanced</mode>
</outbound>
<rule>
<source>
<any />
</source>
<destination>
<network>wanip</network>
<port>63350</port>
</destination>
<protocol>tcp</protocol>
<target>10.100.100.15</target>
<local-port>11</local-port>
<interface>wan</interface>
<descr><![CDATA[delete this]]></descr>
<associated-rule-id />
</rule>
<rule>
<source>
<any />
</source>
<destination>
<network>wanip</network>
<port>63350</port>
</destination>
<protocol>tcp</protocol>
<target>10.100.100.11</target>
<local-port>11</local-port>
<interface>wan</interface>
<descr><![CDATA[don't delete this]]></descr>
<associated-rule-id />
</rule>
</nat>
</pfsense>
I want to remove only the rule element that has the following description:
<descr><![CDATA[delete this]]></descr>
Expected result is the same XDocument (Xd) without the XElement of the condition that will be deleted. I tried this one:
XElement ruleToDelete = null;
foreach (var x in Xd.Root.Element("nat").Elements("rule"))
{
if (x.Element("descr")!= null && x.Element("descr").Value == ruleDescription)
{
ruleToDelete = x;
break;
}
}
if (ruleToDelete != null)
{
ruleToDelete.Remove();
//But Xd is the same as before...
}
But, I want the Xd to be affected...