【发布时间】:2012-01-16 18:04:16
【问题描述】:
我试图从 XElement 中删除后代元素(使用 .Remove()),我似乎得到了一个空对象引用,但我不知道为什么。
看了上一个标题为(see here)的问题,我找到了删除它的方法,但我仍然不明白为什么我尝试第一个的方法不起作用.
有人可以启发我吗?
String xml = "<things>"
+ "<type t='a'>"
+ "<thing id='100'/>"
+ "<thing id='200'/>"
+ "<thing id='300'/>"
+ "</type>"
+ "</things>";
XElement bob = XElement.Parse(xml);
// this doesn't work...
var qry = from element in bob.Descendants()
where element.Attribute("id").Value == "200"
select element;
if (qry.Count() > 0)
qry.First().Remove();
// ...but this does
bob.XPathSelectElement("//thing[@id = '200']").Remove();
谢谢, 罗斯
【问题讨论】:
标签: c# linq-to-xml xelement