gpt4 book ai didi

c# - 如何按属性删除 XML 节点

转载 作者:数据小太阳 更新时间:2023-10-29 02:23:38 26 4
gpt4 key购买 nike

XML

<WorkTable>
<Days>
<Day id="0" name="Monday"/>
<Day id="1" name="Tuesday"/>
<Day id="2" name="Wednesday"/>
<Day id="3" name="Thursday" />
<Day id="4" name="Friday"/>
<Day id="5" name="Saturday"/>
<Day id="6" name="Sunday"/>
</Days>
<SpecialDays>
<Day date="22.07.2015"/>
<Day date="24.07.2015"/>
</SpecialDays>
</WorkTable>

此代码不会从 xml 中删除节点。你能帮我找出问题吗?

XmlDocument doc = new XmlDocument();
doc.Load(localXMLpath + xmlFileName);
XmlNode delNode= doc.SelectSingleNode("/WorkTable/SpecialDays/Day[@date='24.07.2015']");
delNode.ParentNode.RemoveChild(delNode);
doc.Save(localXMLpath + xmlFileName);

最佳答案

这应该有效:

XDocument xdoc = XDocument.Load(filename);
xdoc.Element("WorkTable").Element("SpecialDays").Elements("Day")
.Where(x => (string)x.Attribute("date") == "24.07.2015")
.Remove();
xdoc.Save(filename);

关于c# - 如何按属性删除 XML 节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32438334/

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