【问题标题】:Remove Empty Element using XDocument使用 XDocument 删除空元素
【发布时间】:2014-07-02 04:48:57
【问题描述】:

最初使用 System.Xml.Linq dll 版本 3.5.0.0 我已删除空元素,如下所示

XDocument document = XDocument.Load(_fileName);                      
document.Descendants().Where(e => string.IsNullOrEmpty(e.Value)).Remove();
document.Save(_fileName, SaveOptions.DisableFormatting);

现在我的 System.Xml.Linq dll 版本是 4.0.0.0 但上面的代码不起作用,因为我看不到 Where 子句。

谁能帮助我如何重写代码删除 4.0.0.0 中的空元素

【问题讨论】:

  • 你的代码文件中有using System.Xml.Linq;using System.Linq;吗?

标签: c# xml linq


【解决方案1】:

试试这个,

        var document = XDocument.Parse(original);
        document.Descendants()
       .Where(a=> a.IsEmpty || String.IsNullOrWhiteSpace(a.Value))
       .Remove();

你还应该有以下命名空间,

using System.Linq;
using System.Xml.Linq;

【讨论】:

  • 对不起,正如我在上面提到的,System.Xml.Linq dll 版本是 4.0.0.0 的 Where 子句不存在。这不会工作
  • 我使用 System.Xml.Linq dll 版本 4.0.0.0 编译它
  • 你的项目版本是多少?
  • 项目版本为4.0
  • 兄弟你有添加 system.linq 命名空间吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-08
  • 2019-04-06
  • 2014-04-24
相关资源
最近更新 更多