Using Linq to XML using C#, how do I find attribute values? -


i need activities specific claim id = 13 using linq c#, trying below code, it's not working.

 xdocument doc = xdocument.load(downloadedpath);     var activites = (from e in doc.descendants("activity")                   (string)doc.element("claim").element("id") == 13    

below 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 converting value string comparing int. don't - it's not going work :)

it's simplest convert int instead:

var activites = (from e in doc.descendants("activity")                  (int)doc.element("claim").element("id") == 13                     ...) 

next, @ xml - claim element isn't inside activity element... want:

var activities = doc.descendants("claim")                     .where(claim => (int) claim.element("id") == 13)                     .selectmany(claim => claim.elements("activity")); 

this finds claim elements id of 13 (of there one) , selects activities, using selectmany result sequence of activity elements, rather sequence of sequences...


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -