简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM