我有以下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访问apple 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());
我想我犯了一个错误...我想我也离解决方案不远......
有任何帮助吗?
答案 0 :(得分: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);