I need to get all activities for specific claim ID = 13 using linq C#, I am trying the below code, but it's not working.
XDocument Doc = XDocument.Load(DownloadedPath);
var Activites = (from e in Doc.Descendants("Activity")
where (string)Doc.Element("Claim").Element("ID") == 13
below is the Xml
<?xml version="1.0" encoding="utf-8"?>
<Advice>
<Claim>
<ID>12</ID>
<ProviderID>CL-MC-0012-07</ProviderID>
<Activity>
<ID>20131520320</ID>
<Start>24/07/2013 13:00</Start>
<Type>3</Type>
</Activity>
<Activity>
<ID>20131520321</ID>
<Start>24/07/2013 13:00</Start>
<Type>3</Type>
</Activity>
</Claim>
<Claim>
<ID>13</ID>
<ProviderID>CL-MC-0012-07</ProviderID>
<Activity>
<ID>20132058590</ID>
<Start>20/10/2013 09:00</Start>
<Type>3</Type>
</Activity>
<Activity>
<ID>20132058591</ID>
<Start>20/10/2013 09:00</Start>
<Type>3</Type>
</Activity>
</Claim>
You're currently converting the value to a string
but then comparing it with an int
. Don't do that - it's not going to work :)
It's probably simplest to convert it to an int
instead:
var Activites = (from e in Doc.Descendants("Activity")
where (int)Doc.Element("Claim").Element("ID") == 13
...)
Next, look at your XML - the Claim
element isn't inside an Activity
element... you probably just want:
var activities = doc.Descendants("Claim")
.Where(claim => (int) claim.Element("ID") == 13)
.SelectMany(claim => claim.Elements("Activity"));
This finds all Claim
elements with an ID of 13 (of which there will probably only be one) and then selects its activities, using SelectMany
so that the result will be a sequence of activity elements, rather than a sequence of sequences...
See more on this question at Stackoverflow