1 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以對(duì)這些知名 ID 的列表進(jìn)行匹配/相交。
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 關(guān)注
- 133 瀏覽
添加回答
舉報(bào)