I’m pretty new to this stuff and having a hard time figuring out how to properly access my data. What I have is an XML tree in this form:
<bpm:ResponseData
xmlns:bpm="http://rest.bpm.ibm.com/v1/data">
<status>200</status>
<data
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srch="http://rest.bpm.ibm.com/v1/data/search"
xsi:type="srch:SearchDetails">
<data>
<item key="assignedToUser"/>
<item key="bpdName">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
Some process name
</value>
</item>
<item key="instanceDueDate">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
2011-09-06T12:35:48Z
</value>
</item>
<item key="taskId">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:decimal">
218
</value>
</item>
<item key="taskSubject">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
Task: Some process related task
</value>
</item>
</data>
<data>
<item key="bpdName">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
Another process name
</value>
</item>
<item key="instanceStatus">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
Active
</value>
</item>
<item key="taskId">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:decimal">
253
</value>
</item>
<item key="taskSubject">
<value xmlns:ns5="http://www.w3.org/2001/XMLSchema" xsi:type="ns5:string">
Task: Another process related task
</value>
</item>
</data>
</data>
</bpm:ResponseData>
I need to extract exactly two things from this data: the taskSubject and the taskId. Preferably in a manner which would allow me to iterate over them. Something involving new{subject, id} would be nice.
I’m not quite sure how to handle thing task…
With
var items = from feed in XMLDocument.Descendants("data").Descendants("data") select feed;
I get the two data items. Is there any way to drill them down further, returning the value of the descendant with a specific “key” attribute?
Regards,
Michael
EDIT:
I figured this would work:
var items = from feed in XMLDocument.Descendants("data").Descendants("data") select
new{
subject = from subjects in feed.Elements() where (subjects.Attribute("key").Value=="taskSubject") select subjects.Value,
id = from subjects in feed.Elements() where (subjects.Attribute("key").Value == "taskId") select subjects.Value
};
But that seems pretty “dirty”…
This is a bit hackish, but it should work (tested on Mono 2.10.2):