使用LINQ2XML计算记录

时间:2012-09-28 07:00:29

标签: c# xml razor linq-to-xml

我需要分别计算inbound次交易和outbound次交易。我已经尝试了所有我知道的东西,但没有任何工作。这是XML:

<?xml version="1.0" encoding="utf-8"?>
<records>
    <inbound>
        <transaction>
            <customerID>0002</customerID>
            <customerName>bob</customerName>
        </transaction>
        <transaction>
            <customerID>0003</customerID>
            <customerName>harry</customerName>
        </transaction>
            .
            .
            .
        <transaction>
            <customerID>0250</customerID>
            <customerName>joe</customerName>
        </transaction>
    </inbound>
    <outbound>
        <transaction>
            <customerID>0002</customerID>
            <customerName>bob</customerName>
        </transaction>
        <transaction>
            <customerID>0003</customerID>
            <customerName>harry</customerName>
        </transaction>
            .
            .
            .
        <transaction>
            <customerID>0175</customerID>
            <customerName>frank</customerName>
        </transaction>
    </outbound>
</records>

这需要在C#和LINQ中。谢谢你的帮助。

1 个答案:

答案 0 :(得分:2)

以下是获取inbounds的交易次数的方法:

var xdoc = XDocument.Parse(xml);
var c = xdoc.Descendants("inbound").Descendants("transaction").Count();