LINQ to Object basic删除

时间:2016-02-20 10:34:30

标签: c# linq

我目前有以下代码:

document.createElementNS("http://www.w3.org/2000/svg", "svg")

我期待;

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);

但没有删除。

1 个答案:

答案 0 :(得分:1)

TEST.Elements("Setting")

...只会在根级别找到元素,但由于Settings嵌套在SettingsGroup下,因此更容易在树中所有节点中找到它们使用Descendants

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

...导致......

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