3

我正在从第三方应用程序中提取一个 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"/>

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

请帮帮我。谢谢

4

1 回答 1

2
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);
于 2012-12-05T22:24:20.547 回答