1 回答
TA贡献1829条经验 获得超6个赞
您可以对这些知名 ID 的列表进行匹配/相交。
List<string> ids = new List<string> { "111", "222", "333" };
var result = xml
.Elements("Payment")
.Where(p => {
var invoiceIds = p.Elements("Invoice").Elements("Id").Select(o => o.Value);
return ids.Intersect(invoiceIds).Any();
}
);
完整代码和NetFiddle。
const string XML = @"
<root>
<Payment>
<PaymentId>1</PaymentId>
<Total>50</Total>
<Invoice>
<Id>555</Id>
<Amount>30</Amount>
</Invoice>
<Invoice>
<Id>111</Id>
<Amount>30</Amount>
</Invoice>
</Payment>
<Payment>
<PaymentId>2</PaymentId>
<Total>20</Total>
<Invoice>
<Id>222</Id>
<Amount>20</Amount>
</Invoice>
</Payment>
<Payment>
<PaymentId>3</PaymentId>
<Total>80</Total>
<Invoice>
<Id>888</Id>
<Amount>80</Amount>
</Invoice>
</Payment>
</root>
";
XElement xml = XElement.Parse(XML);
List<string> ids = new List<string> { "111", "222", "333" };
var result = xml
.Elements("Payment")
.Where(p => {
var invoiceIds = p.Elements("Invoice").Elements("Id").Select(o => o.Value);
return ids.Intersect(invoiceIds).Any();
}
);
foreach (var item in result)
{
Console.WriteLine("PaymentId: {0}", (string)item.Element("PaymentId"));
}
- 1 回答
- 0 关注
- 99 浏览
添加回答
举报