c# – 当我们有多个具有相同名称但不同属性的元素时,如何使用Xdo
发布时间:2020-12-15 04:30:06 所属栏目:百科 来源:网络整理
导读:我有一个xml文档,如下所示: Applications myApp add key="ErrorDestinationEventLog" value="EventLog" / add key="version" value="5.0.0.0" / add key="DebugMode_RUN" value="true" / /myApp/Applications 所有元素具有相同的元素名称但不同的属性. 如何
我有一个xml文档,如下所示:
<Applications> <myApp> <add key="ErrorDestinationEventLog" value="EventLog" /> <add key="version" value="5.0.0.0" /> <add key="DebugMode_RUN" value="true" /> </myApp> </Applications> 所有元素具有相同的元素名称但不同的属性. xd.Element("Applications").Element("myApp").Element(xe.Name).RemoveAll(); 上述命令不起作用,因为所有元素都具有相同的名称. 有没有办法识别一个元素,除了它的名字? 解决方法string key = "version"; XDocument xdoc = XDocument.Load(path_to_xml); xdoc.Descendants("add") .Where(x => (string)x.Attribute("key") == key) .Remove(); 更新你几乎做了这份工作.你错过的是按属性值过滤元素.您的代码包含过滤和删除所选元素: xd.Element("Applications") .Element("myApp") .Elements("add") .Where(x => (string)x.Attribute("key") == key) .Remove(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |