`
javatoyou
  • 浏览: 1016429 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

LINQ To XML:设置子元素的值、添加子元素或移除子元素(XElement.SetElementValue)

 
阅读更多

XElement.SetElementValue这个方法相当的体贴,它会自动判断元素是否有该子元素,如果有就是更新,没有就是增加,那么删除呢,也简单得让人开心的笑,XElement.SetElementValue("LastName", null),这句是删除LastName这个元素

输出

对于这个方法,作者也提出一个警示

Just because calling the SetElementValue method with a value of null removes the node, don’t

make the mistake of thinking that manually setting an element’s value to null is the same as removing it in the

LINQ to XML API. This is merely the behavior of the SetElementValue method. If you attempt to set an element’s

value to null using its Value property, an exception will be thrown.

不要天真的认为SetElementValue 方法把一个元素设为NULL是删除,那么你手动把一个元素的值设为NULL就可以删除那个结点

如果你企图设置那个元素的值为NULL,将会抛出一个异常

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics