我有以下XML文件。
<JamStatus>
<IPAddress Value="10.210.104.32 " FacId="2">
<Type>Letter</Type>
<JobId>1</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309464601:144592</TimestampPrinting>
</IPAddress>
<IPAddress Value="10.210.104.32 " FacId="2">
<Type>Letter</Type>
<JobId>2</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309465072:547772</TimestampPrinting>
</IPAddress>
<IPAddress Value="10.210.104.32 " FacId="2">
<Type>Letter</Type>
<JobId>2</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309465072:547772</TimestampPrinting>
</IPAddress>
</JamStatus>
文档中可能有任意数量的IPaddress elememt。对于特定的IPAddress,jobid和timestamp可以是相同的。我想获取其Value,jobid和timestampprinting相同的ipAddress计数。在这种情况下,它是2.这是获取此信息的最佳方式?
有没有使用LINQ的简单方法?
谢谢, SYD
答案 0 :(得分:1)
您可以使用XElement和LINQ:
var s = @"
<JamStatus>
<IPAddress Value=""10.210.104.32 "" FacId=""2"">
<Type>Letter</Type>
<JobId>1</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309464601:144592</TimestampPrinting>
</IPAddress>
<IPAddress Value=""10.210.104.32 "" FacId=""2"">
<Type>Letter</Type>
<JobId>2</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309465072:547772</TimestampPrinting>
</IPAddress>
<IPAddress Value=""10.210.104.32 "" FacId=""2"">
<Type>Letter</Type>
<JobId>2</JobId>
<fi>50-30C-KMC-360A</fi>
<TimestampPrinting>1309465072:547772</TimestampPrinting>
</IPAddress>
</JamStatus>";
XElement xel = XElement.Parse(s);
Console.WriteLine(xel.XPathSelectElements("//IPAddress")
.GroupBy(el => new Tuple<string, string>(el.Element((XName)"JobId").Value, el.Element((XName)"TimestampPrinting").Value))
.Max(g => g.Count())
);
答案 1 :(得分:0)
我会推荐Linq-to-XML。如果我理解正确,您希望找到IPAddress
,JobId
和TimestampPrinting
的集合,其中JobId
和TimestampPrinting
是相同的。为此,您需要group元素。