gpt4 book ai didi

c# - 如何从 XmlNodeList 中删除 XmlNode

转载 作者:IT王子 更新时间:2023-10-29 04:13:40 28 4
gpt4 key购买 nike

我需要根据条件删除 XmlNode。怎么做?

foreach (XmlNode drawNode in nodeList)
{
//Based on a condition
drawNode.RemoveAll(); //need to remove the entire node

}

最佳答案

这应该可以解决问题:

for (int i = nodeList.Count - 1; i >= 0; i--)
{
nodeList[i].ParentNode.RemoveChild(nodeList[i]);
}

如果您使用常规的 for 循环循环,并“向后”循环,您可以随时删除项目。

更新:这里是一个完整的例子,包括加载一个xml文件,定位节点,删除它们并保存文件:

XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("some-xpath-query");
for (int i = nodes.Count - 1; i >= 0; i--)
{
nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(fileName);

关于c# - 如何从 XmlNodeList 中删除 XmlNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/875136/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com