根据子条件移除父节点
Remove the Parent Node based on Child condition
我需要帮助根据某些条件删除 xml 节点
这是我的 xml
<result>
<product>
<auto>
<report>
<auto>
<admin></admin>
<report>
<search>
<subjects>
<subject>
<name>
<first>John</first>
<last>D</last>
</name>
</subject>
</subjects>
</search>
</report>
</auto>
</report>
</auto>
<auto>
<report>
<auto>
<admin></admin>
<report>
<search>
<subjects>
<subject>
<name>
<first>Jack</first>
<last>L</last>
</name>
</subject>
</subjects>
</search>
</report>
</auto>
</report>
</auto>
</product>
</result>
从这个 xml 中,根据名字和姓氏,删除 "auto" 节点的其余部分
如果名字 = John 且姓氏 = D
,则保留 "auto" 节点
预期 xml:
<result>
<product>
<auto>
<report>
<auto>
<admin></admin>
<report>
<search>
<subjects>
<subject>
<name>
<first>John</first>
<last>D</last>
</name>
</subject>
</subjects>
</search>
</report>
</auto>
</report>
</auto>
</product>
</result>
我想先通过
提取所需的内容
var query =
from p in XDocument.Parse(myXml).Root.Elements ("result/product/auto/report/auto/report/search/subjects/subject/name")
where (
from c in p.Elements("first")
where c.Value == "John"
select c
).Any()
select p;
Please suggest me here.
如果我理解正确的话:
string first = "John";
string last = "D";
XDocument xd = XDocument.Parse(xml);
xd.Root.Element("product").Elements("auto").ToList()
.ForEach(x=>
{
var name = x.Descendants("name").First();
if (name.Element("first").Value != first
&& name.Element("last").Value != last)
x.Remove();
});
Console.WriteLine(xd);
打印:
<result>
<product>
<auto>
<report>
<auto>
<admin></admin>
<report>
<search>
<subjects>
<subject>
<name>
<first>John</first>
<last>D</last>
</name>
</subject>
</subjects>
</search>
</report>
</auto>
</report>
</auto>
</product>
</result>
我需要帮助根据某些条件删除 xml 节点
这是我的 xml
<result> <product> <auto> <report> <auto> <admin></admin> <report> <search> <subjects> <subject> <name> <first>John</first> <last>D</last> </name> </subject> </subjects> </search> </report> </auto> </report> </auto> <auto> <report> <auto> <admin></admin> <report> <search> <subjects> <subject> <name> <first>Jack</first> <last>L</last> </name> </subject> </subjects> </search> </report> </auto> </report> </auto> </product> </result>
从这个 xml 中,根据名字和姓氏,删除 "auto" 节点的其余部分
如果名字 = John 且姓氏 = D
,则保留 "auto" 节点预期 xml:
<result> <product> <auto> <report> <auto> <admin></admin> <report> <search> <subjects> <subject> <name> <first>John</first> <last>D</last> </name> </subject> </subjects> </search> </report> </auto> </report> </auto> </product> </result>
我想先通过
提取所需的内容var query = from p in XDocument.Parse(myXml).Root.Elements ("result/product/auto/report/auto/report/search/subjects/subject/name") where ( from c in p.Elements("first") where c.Value == "John" select c ).Any() select p;
Please suggest me here.
如果我理解正确的话:
string first = "John";
string last = "D";
XDocument xd = XDocument.Parse(xml);
xd.Root.Element("product").Elements("auto").ToList()
.ForEach(x=>
{
var name = x.Descendants("name").First();
if (name.Element("first").Value != first
&& name.Element("last").Value != last)
x.Remove();
});
Console.WriteLine(xd);
打印:
<result>
<product>
<auto>
<report>
<auto>
<admin></admin>
<report>
<search>
<subjects>
<subject>
<name>
<first>John</first>
<last>D</last>
</name>
</subject>
</subjects>
</search>
</report>
</auto>
</report>
</auto>
</product>
</result>