[英]How to convert list of XElement to XDocument?
My work is to remove some nodes from an XML based upon certain conditions and then parse the changed XML to a string. 我的工作是根据某些条件从XML中删除一些节点,然后将更改后的XML解析为字符串。
To do that: 要做到这一点:
I converted string(workingData)to Xdocument and then added a condition to select xelements (abc) which works fine. 我将字符串(workingData)转换为Xdocument,然后添加了一个条件以选择效果良好的xelements(abc)。
Now I need help to replace existing xelements with the new xelement(abc) back in doc with the removed nodes . 现在,我需要帮助,以在doc中使用已删除的节点用新的xelement(abc)替换现有的xelements 。
PS: Ignore the hardcoded return "xx" that will be changed later to the converted xml. PS:忽略硬编码的返回值“ xx”,以后将其更改为转换后的xml。
private string convertXML(string workingData, IEnumerable<string> fulfillerClaims)
{
XDocument doc = XDocument.Parse(workingData);
IEnumerable<XElement> abc;
foreach (XElement item in doc.Elements())
{
abc = item.Elements().Where(x => x.Name.LocalName == "Entry").Select(x => x).Where(x => x.Attribute("Fulfiller") == null || fulfillerClaims.Contains(x.Attribute("Fulfiller").Value));
}
return "xx";
}
You could just remove the elements you don't want? 您可以删除不需要的元素吗? I think this would work: 我认为这会起作用:
foreach (XElement item in doc.Elements()) {
item.Elements().Where(x => x.Name.LocalName != "Entry" && (x.Attribute("Fulfiller") != null && !fulfillerClaims.Contains(x.Attribute("Fulfiller").Value))).Remove();
}
Might want to check the conditional logic on this.. I didn't have any test data to work with... 可能想检查条件逻辑。.我没有任何测试数据可以使用...
I did remove parts of it like this that just seemed to be wasting cycles: 我确实删除了似乎浪费时间的部分代码:
.Select(x => x)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.