0

我有以下 XML 文件:

<tree>
    <branchs>
         <branch id=1>
             <apple id=1 color=green/>
             <apple id=2 color=red/>
         </branch>
         <branch id=2>
             <apple id=1 color=green/>
             <apple id=2 color=red/>
         </branch>
    </branchs>
</tree>

我希望 SQL 命令从分支 id 1 访问苹果 id#1,而不是更改颜色(第一次),然后能够从这个分支中删除这个苹果。

我尝试了以下方法来删除一个苹果,但没有任何结果

XDocument doc = XDocument.Load(myxmlFile);
var result = (from selectedApples in 
                (from selectedBranch in doc.Element("Branchs").Elements("Branch) 
                where selectedBranch.Attribute("id").Value == 1 
                select selectedBranch)
                where selectedApples.Attribute("id").Value == 1
                select selectedApples).ToList();

 result.ToList().ForEach(apple => apple.Remove());

我想我犯了一个错误......我想我也离解决方案不远了......

有什么帮助吗?

4

1 回答 1

0

好吧,不确定它是否是优化的解决方案......但它正在工作。

XDocument doc = XDocument.Load(myxmlFile);
var results = from selectedApples in doc.Root.Element("branchs").Descendants()
                where selectedApples.Attribute("id").Value == 1
                select selectedApples.Elements("apple");

foreach(var result in results)
    result.Where(a => a.Attribute("id").Value == 1).ToList().Foreach(a => a.Remove());

doce.Save(myxmlFile);
于 2012-11-22T14:42:58.597 回答