c# - Get another attribute on the same element by searching another attribute -
c# - Get another attribute on the same element by searching another attribute -
this might simplest question appologise. part of xml. want attribute "value" based on attribute "itemoid". ie when itemoid="i_medic_history_indication" value.
<formdata formoid="f_neurological_v20" openclinica:version="v2.0" openclinica:status="initial info entry"> <itemgroupdata itemgroupoid="ig_neuro_ungrouped" itemgrouprepeatkey="1" transactiontype="insert"> <itemdata itemoid="i_neuro_neuro_tendonab" value="" /> <itemdata itemoid="i_neuro_neuro_tendon" value="1" /> <itemdata itemoid="i_neuro_neuro_gaitab" value="" /> <itemdata itemoid="i_neuro_neuro_gait" value="1" /> <itemdata itemoid="i_neuro_neuro_coordinateab" value="" /> <itemdata itemoid="i_neuro_neuro_coordinate" value="1" /> <itemdata itemoid="i_neuro_neuro_sensoryab" value="" /> <itemdata itemoid="i_neuro_neuro_sensory" value="1" /> <itemdata itemoid="i_neuro_neuro_muscletoneab" value="" /> <itemdata itemoid="i_neuro_neuro_muscletone" value="1" /> <itemdata itemoid="i_neuro_neuro_motorab" value="" /> <itemdata itemoid="i_neuro_neuro_motor" value="1" /> <itemdata itemoid="i_neuro_neuro_cranialab" value="" /> <itemdata itemoid="i_neuro_neuro_cranial" value="1" /> <itemdata itemoid="i_neuro_neuro_speechab" value="" /> <itemdata itemoid="i_neuro_neuro_speech" value="1" /> <itemdata itemoid="i_neuro_neuro_behaveab" value="abnormal" /> <itemdata itemoid="i_neuro_neuro_behave" value="0" /> <itemdata itemoid="i_neuro_neuro_appearab" value="" /> <itemdata itemoid="i_neuro_neuro_appear" value="1" /> <itemdata itemoid="i_neuro_neuro_normal" value="1" /> <itemdata itemoid="i_neuro_neuro_date" value="2014-10-10" /> </itemgroupdata> </formdata>
this code. can tell bit stuck.
string neuroexampath = "//oc:formdata[@formoid='f_neurological_v20']/oc:itemgroupdata/oc:itemdata"; var neuroexamlist = doc.xpathselectelements(neuroexampath, nsmgr).reverse(); foreach (var neuroexamelement in neuroexamlist) { var patrow = patdt.newrow(); var i_neuro_neuro_normal = neuroexamelement.attribute("value").value; patrow["neuro_normal"] = i_neuro_neuro_normal; var i_neuro_neuro_appear = neuroexamelement.attribute("value").value; patrow["neuro_appear"] = i_neuro_neuro_appear; var i_neuro_neuro_appearab = neuroexamelement.attribute("value").value; patrow["neuro_appearab"] = i_neuro_neuro_appearab; patdt.rows.add(patrow); }
as can tell iterate attribute value time. need var i_neuro_neuro_normal = neuroexamelement.attribute("value").value; line have status itemoid="i_neuro_neuro_normal" , on. can set 1 attribute @ time datatable. please help. give thanks you.
it looks me want new row each itemgroupdata
element, not each itemdata
element. expect like:
foreach (var itemgroup in doc.root.elements("itemgroupdata")) { var row = patdt.newrow(); row["neuro_normal"] = itemgroup.extractvalue("i_neuro_neuro_normal"); row["neuro_appear"] = itemgroup.extractvalue("i_neuro_neuro_appear"); row["neuro_appearab"] = itemgroup.extractvalue("i_neuro_neuro_appearab"); patdt.rows.add(row); }
which uses new extension method:
public static string extractvalue(this xelement parent, string oid) { homecoming parent.elements("itemdata") .where(x => x.attribute("itemoid").value == oid) .first() .attribute("value") .value; }
alternatively, if you're extracting lot of attributes, build dictionary:
var values = itemgroup.elements("itemdata") .todictionary(x => x.attribute("itemoid").value, x => x.attribute("value").value);
c# linq xpath xelement xattribute
Comments
Post a Comment