Reputation:
I have this XML file:
<XtraSerializer version="1.0" application="View">
<property name="Columns" iskey="true" value="23">
<property name="Item23" isnull="true" iskey="true">
<property name="Name">colworkspace</property>
<property name="Width">75</property>
<property name="MinWidth">20</property>
<property name="MaxWidth">0</property>
</property>
</property>
<property name="FormatRules" iskey="true" value="1">
<property name="Item1" isnull="true" iskey="true">
<property name="ColumnName">colid</property>
<property name="Name">Format0</property>
<property name="RuleType">#FormatConditionRuleExpression</property>
<property name="Rule" isnull="true" iskey="true">
<property name="Expression">[id] > 1L</property>
<property name="Appearance" isnull="true" iskey="true">
<property name="Options" isnull="true" iskey="true">
<property name="UseForeColor">true</property>
</property>
<property name="ForeColor">195, 214, 155</property>
</property>
</property>
</property>
</property>
</XtraSerializer>
It has two properties, Columns
and FormatRules
. I want to delete the Columns
property and keep the FormatRules
property. What I have done is creating a method that deletes all elements which does not have name
= FormatRules
but that will delete all the children of the FormatRules
property too, which I don't want. This is my code:
XDocument doc = XDocument.Load(path);
IEnumerable<XElement> element = from node in doc.Descendants("property")
let attr = node.Attribute("name")
where attr != null && attr.Value != "FormatRules"
select node;
element.ToList().ForEach(x => x.Remove());
doc.Save(path);
This will result in this XML file:
<XtraSerializer version="1.0" application="View">
<property name="FormatRules" iskey="true" value="1">
</XtraSerializer>
Upvotes: 0
Views: 675
Reputation: 273621
//IEnumerable<XElement> element = from node in doc.Descendants("property")
IEnumerable<XElement> element = from node in doc.Root.Elements("property")
Upvotes: 1