c# - How to find a list of Elemenets/Attributes from a specific Element in XML? -
how can query xml document return list of sub-elements/attributes of specific element.
var entry = item in doc.descendants("group") (string)item.attribute("meetsmark") == "1" select new { code = (string)item.attribute("code"), };
the above returns entries belong element group, meetsmark attribute "1". each of elements returns code need list of 'sum' , 'number' every summary attribute contains. example: need have query return:
code = secondary
sum = sum1
number = 1
sum = sum3
number = 3
sum = sum4
number = 4
code =third
sum = sum10
number = 1
sum = sum30
number = 3
sum = sum40
number = 4
below xml structure.
<group id="grp2" code="main" meetsmark="0" <group id="grp3" code="secondary" meetsmark="1" <items> <item id="itm6" <summary sum="sum1" number="1" </summary> </item> <item id="itm14" <summary sum="sum3" number="3" </summary> </item> <item id="itm15" <summary sum="sum4" number="4" </summary> </item> </items> </group> <group id="grp4" code="third" meetsmark="1" <items> <item id="itm95" <summary sum="sum10" number="1" </summary> </item> <item id="itm96" <summary sum="sum30" number="3" </summary> </item> <item id="itm97" <summary sum="sum40" number="4" </summary> </item> </items> </group> </group>
it ... (line feeds omitted clarity)
var groups = group in doc.descendants("group") (string)group.attribute("meetsmark") == "1" select group var subgroups = subgroup in groups.descendants("group") subgroup.attribute("code").value == "secondary" select subgroup; foreach( var subgroup in subgroups ) { system.console.writeformat( "code = {0}", subgroup.attribute("code").value ); var sums = summary in subgroup.descendants("summary") foreach( var sum in sums ) { system.console.writeformat( "sum = {0}", subgroup.attribute("sum").value ); system.console.writeformat( "number = {0}", subgroup.attribute("number").value ); } }
Comments
Post a Comment