c# - 如果找到自闭标签,如何删除元素

标签 c#

我正在从第三方应用程序中提取一个 xml 文件,如下所示

<round round_id="14208" name="5th Round" start_date="2013-09-06" end_date="2013-09-10" type="cup" groups="0" has_outgroup_matches="no" last_updated="2011-04-05 14:15:02">
    --Some Data with more elements
</round>

<round round_id="14208" name="5th Round" start_date="2013-09-06" end_date="2013-09-10" type="cup" groups="0" has_outgroup_matches="no" last_updated="2011-04-05 14:15:02">
    --Some Data with more elements
</round>

我的问题是有时这个标签带有这样的自关闭标签

<round round_id="14208" name="5th Round" start_date="2013-09-06" end_date="2013-09-10" type="cup" groups="0" has_outgroup_matches="no" last_updated="2011-04-05 14:15:02"/>

我想检查自闭标签并删除该元素。

请帮帮我。 谢谢

最佳答案

var file = "C:\\YourPath\\data.xml";
var doc = new XmlDocument();
doc.Load(file);
var nodes = doc.SelectNodes("/rounds/round");
foreach (XmlNode item in nodes)
{
    if (item.InnerXml == String.Empty)
    {
        item.ParentNode.RemoveChild(item);
    }
}
doc.Save(file);

关于c# - 如果找到自闭标签,如何删除元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10208945/

相关文章:

c# - 我可以使用反射类型作为类型参数吗?

c# - 如何在 C# 方法中返回 2 个值

C# Entity Framework - Order by and Take

c# - Web 应用程序项目 [...] 配置为使用 IIS。找不到 Web 服务器 [...]。

c# - 为整数键控模型集合寻找优雅/高效的解决方案

c# - 在具有多种对象类型的 TreeView 中处理 SelectedItemChanged

c# - 寻找有关如何创建增强型工具提示的建议

c# - Razor ASP.NET : Show data from 'if' inside 'foreach' . 'break;' 不工作

c# - 何时强制执行 LINQ 查询评估?

c# - 如何使用 JSON.NET "flatten"一些 JSON?