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);
答案 0 :(得分:3)
这应该有效:
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);
答案 1 :(得分:2)
您的代码运行正常,问题是您正在尝试覆盖您从中读取数据的文件。