c# - Find Values using Linq -


i have string value shown below. how return values contained in quotes. below answer 70116280,05/06/2014 16:31:38,etc......essentially answer contained in quotes fired list.

string value

<!-- capacitor stack signoff record --> <stack_signoff_result lead_traveler="70116280" date="05/06/2014 16:31:38"      employee_id="testuser" total_energy="77.266345" status="true"      operation_mode="manual" result_detail="na">   <signoff_data>     <adhesive part_number="1234" kanban_number="5678"/>   </signoff_data>   <capacitor_set>     <top_capacitor traveler="45911012" energy="26.000567" capacitance="0.000287553595"/>     <middle_capacitor traveler="45817588" energy="25.576334" capacitance="0.00028426705"/>     <bottom_capacitor traveler="45911141" energy="25.689444" capacitance="0.000284239714"/>   </capacitor_set> </stack_signoff_result> 

i think easiest way use xpath queires:

using system.linq; using system.xml.linq; using system.xml.xpath;  ...  xdocument document = xdocument.parse(stringvalue);  var xpath = @"//stack_signoff_result[@lead_traveler='70116280']"; var result = item in document.xpathselectelements(xpath)     select item;  foreach (var item in result) {     ... } 

update: quoted attributes values can use linq oneliner below (i have formatted readability, can flatten code single line).

var result = string.join(     ",",     xelement         // parse xml or can reuse element found in code above         .parse(string value)         // search in element , descendants         .descendantsandself()         // select atributtes (quoted values)         .selectmany(item => item.attributes())         // select attribute value         .select(item => item.value) ); 

this solution not fast string manipulations, more stable. example, if in code of enigmativity quotes inside text node, you'll wrong results.

also more easy mix search task you've written in first version:

var result = xdocument     .parse(stringvalue)     .xpathselectelements(@"//stack_signoff_result[@lead_traveler='70116280']")     .select(item =>         string.join(             ",",             item.descendantsandself()             .selectmany(element => element.attributes())             .select(element => element.value)         )     ) ; 

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -