XDocument 删除节点
我有一个 XML 文件,
<rows>
<head>
<beforeInit>
<call command="attachHeader">
<param>#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter</param>
</call>
</beforeInit>
<afterInit>
<call command="enablePaging">
<param>recinfoArea</param>
</call>
</afterInit>
<column width="100" align="center" type="ro" sort="server" color="undefined" id="Id">Id</column>
<column width="100" align="center" type="ro" sort="server" color="undefined" id="NazovProjektu">NazovProjektu</column>
</head>
</rows>
我想删除 beforeInit 和 afterInit 元素。
我尝试过
xml.Elements().Where(e=>e.Name == "beforeInit" || e.Name == "afterInit").Remove();
但没有运气。
I have a XML file
<rows>
<head>
<beforeInit>
<call command="attachHeader">
<param>#text_filter,#text_filter,#text_filter,#text_filter,#text_filter,#text_filter</param>
</call>
</beforeInit>
<afterInit>
<call command="enablePaging">
<param>recinfoArea</param>
</call>
</afterInit>
<column width="100" align="center" type="ro" sort="server" color="undefined" id="Id">Id</column>
<column width="100" align="center" type="ro" sort="server" color="undefined" id="NazovProjektu">NazovProjektu</column>
</head>
</rows>
I'd like to remove the beforeInit and afterInit elements.
I tried
xml.Elements().Where(e=>e.Name == "beforeInit" || e.Name == "afterInit").Remove();
but no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你想删除每次出现的 beforeInit 或 afterInit ,你可以使用
(后代而不是元素)。
elements() 返回直接子节点的列表,而后代则返回每个节点。
if you want to delete every occurence of beforeInit or afterInit you could use
(descendants instead of elements).
elements() returns a list of direct child nodes, whereas descendants returns every node.
如果 xml 是 XElement,请尝试:
否则,如果它是 XDocument:
按照现在的方式,它被设置为在
中查找子元素,而不是;
。换句话说,Elements() 只返回节点的直接子节点。如果您想要所有后代,无论级别如何,您都需要 Descendants()。If xml is a XElement, try:
Otherwise, if it's an XDocument:
The way it is now, it's set up to look for the sub elements in
<rows>
, not<head>
. In other words Elements() only returns the direct children of a node. If you want all the descendants, no matter what level, you want Descendants().