删除内部节点,但不与XML库的XDocument在C#.NET中的价值 [英] Delete an inner node but not the value in xml with XDocument library in C# .NET

查看:115
本文介绍了删除内部节点,但不与XML库的XDocument在C#.NET中的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的XML文件:

I have the following XML file:

<?xml version="1.0" encoding="utf-8"?>
<html>
  <body>
    <p><span class="screenitems">Add</span></p>
  </body>
</html>



我想删除<跨度> 节点,但在添加来存在的,所以它看起来在最后如下:

I Want it to delete <span> node but the Add to exist, so it looks as follows at the end:

<?xml version="1.0" encoding="utf-8"?>
<html>
  <body>
    <p>Add</p>
  </body>
</html>



是否有一个良好/简单的方法来做到这一点?

Is there a good/simple way to do this?

推荐答案

使用 ReplaceWith ,例如:

XDocument doc = XDocument.Load("file.xml");
XElement span = doc.Descendants("p").First().Elements("span").FirstOrDefault(s => (string)s.Attribute("class") == "screenitems");
if (span != null) 
{
  span.ReplaceWith(span.Nodes());
}

这篇关于删除内部节点,但不与XML库的XDocument在C#.NET中的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆