【问题标题】:Count records using LINQ2XML使用 LINQ2XML 计数记录
【发布时间】:2023-03-31 10:18:01
【问题描述】:

我需要分别获取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 中。感谢您的帮助。

【问题讨论】:

  • 你应该展示你尝试过的代码
  • 剃须刀和这个有什么关系?
  • @CuongLe 你说得对,但我尝试过的代码现在一团糟,我真的很累。
  • @MichalKlouda 这一切都进入了一个网页。我想我不需要为 Razor 标记它。

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


【解决方案1】:

这里是如何获取入站的交易数量:

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

【讨论】:

  • 非常感谢。几个小时以来,我一直在努力解决这个问题。好吧,是时候为自己没有看到简单的方法而自责了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多