xDocument删除元素基于子元素值

时间:2019-05-31 14:07:36

标签: c# linq-to-xml

我正尝试根据其子元素值从xml文件中删除该元素。

我的xml格式如下: my xml format

如果CB元素的子元素CBA具有AXIS的值,我想删除它。

这是我正在尝试的方法,编译器没有给我任何错误,但也没有删除元素。

string portXML = @"C:\Users\User\Desktop\port.xml";
XDocument _port = XDocument.Load(portXML);
_port.Descendants().Where(e => e.Name("CBA").Value == "AXIS").Remove();
_port.Save(portXML);

我不熟悉属性/元素和xDoc的方式,因此我很抱歉这是一个愚蠢的问题。

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

string portXML = @"C:\Users\User\Desktop\port.xml";
            XDocument _port = XDocument.Load(portXML);
             _port.Descendants("CB").Where(e => e.Element("CBA").Value == "AXIS").Remove();
            _port.Save(portXML);
相关问题