Jason
Jason

Reputation: 371

LINQ to Object basic Remove

I've currently got the following code:

XElement TEST = XElement.Parse(
    @"<SettingsBundle>
        <SettingsGroup Id=""QAVerificationSettings"">
            <Setting Id=""RegExRules"">True</Setting>
            <Setting Id=""RegExRules123"">
                <RegExRule xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"" xmlns=""http://schemas.datacontract.org/2004/07/Sdl.Verification.QAChecker.RegEx"">
                    <IgnoreCase>false</IgnoreCase>
                </RegExRule>
            </Setting>
        </SettingsGroup>
    </SettingsBundle>");
TEST.Elements("Setting").Remove();  
var NEW = new XDocument( TEST ); 
var OUT = Directory.GetCurrentDirectory() + @"\_Texting.xml";
NEW.Save(OUT);

I'm expecting;

<SettingsBundle>
<SettingsGroup Id=""QAVerificationSettings"">
</SettingsGroup>
</SettingsBundle>

But nothing is deleted.

Upvotes: 0

Views: 22

Answers (1)

Joachim Isaksson
Joachim Isaksson

Reputation: 180887

TEST.Elements("Setting")

...will only find elements on the root level, but since Settings is nested under SettingsGroup, it's easier to just find them among all nodes in the tree using Descendants.

TEST.Descendants("Setting").Remove();  

...which results in...

<SettingsBundle>
  <SettingsGroup Id="QAVerificationSettings" />
</SettingsBundle>

Upvotes: 1

Related Questions