我已将类似于以下内容的Xml加载到XDocument
中<requestlist>
<request name="Apple" jobtype="radio">
<schedule intervalminutes="1440" daily="true" updateDateTime="2014-08-07T15:43:00Z">
<channel>someData/channel>
<signal>someData</readysignal>
<frequency>someData</frequency>
</schedule>
<file>someData</file>
<file>someData</file>
</request>
</requestlist>
我现在想要添加第二个请求,该请求首先包含一些信息并删除一些不需要的元素,例如
<request name="newBanana">
<schedule intervalminutes="1440" daily="true" updateDateTime="2014-08-07T15:43:00Z">
</schedule>
<file>someData</file>
</request>
是否有一种简单的方法可以通过匹配列表来使用Linq删除元素 e.g。
using System.Xml.XPath;
XDocument configXml = XDocument.Parse(theXml)
var oldRequest = configXml.XPathSelectElement(@"/requestlist/request[@name=""Apple""]");
var nodesToRemove = new List<XName>() {"channel", "signal", "radio"};
var newRequest = oldRequest.Elements().Select(e => e.Name).Intersect(nodesToRemove); //OR something
答案 0 :(得分:0)
首先让您的XPath在<schedule>
元素而不是<request>
停止,因为您将在此处删除<schedule>
的孩子:
var oldRequest = doc.XPathSelectElement(@"/requestlist/request[@name='Apple']/schedule");
然后是的,您可以使用简单的LINQ删除与列表中的名称匹配的元素:
var nodesToRemove = new List<XName>() { "channel", "signal", "radio" };
oldRequest.Elements()
.Where(o => nodesToRemove.Contains(o.Name))
.Remove();