我给出代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<contractPerson>
<person ID="123">
<name>王占华</name>
<phoneNumber>123</phoneNumber>
<sex>男</sex>
<department>信息部</department>
</person>
<person ID="15829922842">
<name>齐总</name>
<phoneNumber>15829922842</phoneNumber>
<sex>帅哥</sex>
<department>国防部</department>
</person>
</contractPerson>
c#代码:
XElement xml = XElement.Load(@"D:\documents and settings\administrator\my documents\visual studio 2010\Projects\IuseSoftSMSCat\IuseSoftSMSCat\bin\Debug\Config\ContractPerson.xml");
IEnumerable<XElement> deleteNode = from item in xml.Descendants("contractPerson")
where item.Attribute("id").Value.Equals("15829922842")
select item;
deleteNode.Remove();
xml.Save(@"D:\documents and settings\administrator\my documents\visual studio 2010\Projects\IuseSoftSMSCat\IuseSoftSMSCat\bin\Debug\Config\ContractPerson.xml");
路径正确,每次运行完,提示文件在编辑器外被更改,可是打开并没有删除,咋回事?
xml:
<?xml version="1.0" encoding="utf-8"?>
<contractPerson>
<person ID="123">
<name>王占华</name>
<phoneNumber>123</phoneNumber>
<sex>男</sex>
<department>信息部</department>
</person>
<person ID="15829922842">
<name>齐总</name>
<phoneNumber>15829922842</phoneNumber>
<sex>帅哥</sex>
<department>国防部</department>
</person>
</contractPerson>
c#代码:
XElement xml = XElement.Load(@"D:\documents and settings\administrator\my documents\visual studio 2010\Projects\IuseSoftSMSCat\IuseSoftSMSCat\bin\Debug\Config\ContractPerson.xml");
IEnumerable<XElement> deleteNode = from item in xml.Descendants("contractPerson")
where item.Attribute("id").Value.Equals("15829922842")
select item;
deleteNode.Remove();
xml.Save(@"D:\documents and settings\administrator\my documents\visual studio 2010\Projects\IuseSoftSMSCat\IuseSoftSMSCat\bin\Debug\Config\ContractPerson.xml");
路径正确,每次运行完,提示文件在编辑器外被更改,可是打开并没有删除,咋回事?
解决方案 »
- HttpWebRequest post 提交重复数据
- 两个gridview控件联动的问题
- c#怎样判断不同的数据库
- 当前上下文中不存在名称“video
- 求助!!!我用C#语言,通过Socket同作为客户端的一个芯片进行通信的问题
- C#操作DBF删除记录的问题
- TransationScope事务不撤消,微软的BUG吗???
- c#新手提问 -- C#连接oracle报错:ORA-12560: TNS:protocol adapter error,有经验进来指点一下,up有分.
- 怎么做一个网页
- 请教各位大虾自动完成问题
- 选中datagridview中的内容,按下ctrl+c进行复制,粘贴到EXCEL中中文乱码
- 关于写try catch finally的疑惑
contractPerson元素并没有id属性,运行会报错的你确定你要删除的是contractPerson元素而不是person元素?[code=c#]
XDocument doc = XDocument.Load("test.xml");
IEnumerable<XElement> deleteNode = from item in doc.Descendants("person")
where item.Attribute("ID").Value.Equals("15829922842")
select item;
deleteNode.Remove();
doc.Save("test.xml");
[code]
区分大小写?
[code]
IEnumerable<XElement> deleteNode = from item in doc.Descendants("person")
where item.Attribute("ID").Value.Equals("15829922842")
select item;
deleteNode.Remove();
doc.Save("test.xml");
文件被按原样重写了一边