Filter data from an XML document
I have the following XML file.
<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>
There may be any number of IPaddress elememt in the document. jobid and timestamp can be same for a perticular IPAddress. I want to get the count of ipAddress whose Value, jobid and timestampprinting are same. In this case it is 2. which is the best way to get this information?
Is there any simple method without using LINQ?
Thanks, syd
You can use XElement and 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())
);
I would recommend Linq-to-XML. If I'm understanding correctly, you want to find the sets of IPAddress
, JobId
and TimestampPrinting
where JobId
and TimestampPrinting
are the same. For that, you need to group the elements together.
精彩评论