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