c# - Parsing XML - LINQ to XML or any other method -
i need extract thevalue of "file_name" attribute , innertext of the attributes values "m5" & "m6". name of file , number of classes , methods present in it. parsing method fine me. tried ways , extract file_name can't further down metric ids.. please help
<?xml version="1.0" encoding="utf-8" ?> <sourcemonitor_metrics> <project version="3.4"> <project_name></project_name> <project_directory>e:\dev_2012\test\src</project_directory> <project_language>java</project_language> <ignore_headers_footers>false</ignore_headers_footers> <export_raw_numbers>false</export_raw_numbers> <metric_names name_count="15"> <metric_name id="m0" type="number">lines</metric_name> <metric_name id="m1" type="number">statements</metric_name> <metric_name id="m2" type="percent" divisor="m1">percent branch statements</metric_name> <metric_name id="m3" type="number">method call statements</metric_name> <metric_name id="m4" type="percent" divisor="m0">percent lines comments</metric_name> <metric_name id="m5" type="number">classes , interfaces</metric_name> <metric_name id="m6" type="ratio" divisor="m5">methods per class</metric_name> <metric_name id="m7" type="ratio" divisor="m6">average statements per method</metric_name> <metric_name id="m8" type="string">line number of complex method</metric_name> <metric_name id="m9" type="string">name of complex method</metric_name> <metric_name id="m10" type="maximum">maximum complexity</metric_name> <metric_name id="m11" type="string">line number of deepest block</metric_name> <metric_name id="m12" type="maximum">maximum block depth</metric_name> <metric_name id="m13" type="average" values="block_depths">average block depth</metric_name> <metric_name id="m14" type="average">average complexity</metric_name> </metric_names> <checkpoints checkpoint_count="1"> <checkpoint checkpoint_name="baseline" checkpoint_date="2014-03-05" ignore_blank_lines="false" modified_complexity="false"> <files file_count="374"> <file file_name="com\cs\batch\tasks\test1"> <metrics metric_count="15"> <metric id="m0">572</metric> <metric id="m1">408</metric> <metric id="m2">14.0</metric> <metric id="m3">288</metric> <metric id="m4">4.4</metric> <metric id="m5">1</metric> <metric id="m6">23.00</metric> <metric id="m7">14.91</metric> <metric id="m8">308</metric> <metric id="m9">method1</metric> <metric id="m10">18</metric> <metric id="m11">380</metric> <metric id="m12">6</metric> <metric id="m13">2.71</metric> <metric id="m14">3.83</metric> </metrics> <block_depths depth_count="10"> <block_depth level="0">29</block_depth> <block_depth level="1">36</block_depth> <block_depth level="2">126</block_depth> <block_depth level="3">93</block_depth> <block_depth level="4">92</block_depth> <block_depth level="5">27</block_depth> <block_depth level="6">7</block_depth> <block_depth level="7">0</block_depth> <block_depth level="8">0</block_depth> <block_depth level="9+">0</block_depth> </block_depths> </file> <file file_name="com\cs\batch\tasks\test2"> <metrics metric_count="15"> <metric id="m0">446</metric> <metric id="m1">322</metric> <metric id="m2">10.6</metric> <metric id="m3">215</metric> <metric id="m4">6.1</metric> <metric id="m5">1</metric> <metric id="m6">14.00</metric> <metric id="m7">18.71</metric> <metric id="m8">184</metric> <metric id="m9">method2</metric> <metric id="m10">11</metric> <metric id="m11">171</metric> <metric id="m12">8</metric> <metric id="m13">2.98</metric> <metric id="m14">4.43</metric> </metrics> <block_depths depth_count="10"> <block_depth level="0">31</block_depth> <block_depth level="1">29</block_depth> <block_depth level="2">38</block_depth> <block_depth level="3">140</block_depth> <block_depth level="4">39</block_depth> <block_depth level="5">16</block_depth> <block_depth level="6">32</block_depth> <block_depth level="7">3</block_depth> <block_depth level="8">1</block_depth> <block_depth level="9+">0</block_depth> </block_depths> </file> <file file_name="com\cs\batch\tasks\test3"> <metrics metric_count="15"> <metric id="m0">2195</metric> <metric id="m1">1341</metric> <metric id="m2">3.8</metric> <metric id="m3">1666</metric> <metric id="m4">17.6</metric> <metric id="m5">4</metric> <metric id="m6">4.50</metric> <metric id="m7">67.78</metric> <metric id="m8">171</metric> <metric id="m9">method3</metric> <metric id="m10">14</metric> <metric id="m11">975</metric> <metric id="m12">9+</metric> <metric id="m13">6.33</metric> <metric id="m14">4.07</metric> </metrics> <block_depths depth_count="10"> <block_depth level="0">58</block_depth> <block_depth level="1">48</block_depth> <block_depth level="2">141</block_depth> <block_depth level="3">171</block_depth> <block_depth level="4">42</block_depth> <block_depth level="5">57</block_depth> <block_depth level="6">61</block_depth> <block_depth level="7">11</block_depth> <block_depth level="8">36</block_depth> <block_depth level="9+">721</block_depth> </block_depths> </file> <file file_name="com\cs\batch\tasks\test4"> <metrics metric_count="15"> <metric id="m0">202</metric> <metric id="m1">146</metric> <metric id="m2">5.5</metric> <metric id="m3">100</metric> <metric id="m4">2.5</metric> <metric id="m5">1</metric> <metric id="m6">9.00</metric> <metric id="m7">10.78</metric> <metric id="m8">51</metric> <metric id="m9">method4</metric> <metric id="m10">4</metric> <metric id="m11">58</metric> <metric id="m12">5</metric> <metric id="m13">2.00</metric> <metric id="m14">2.00</metric> </metrics> <block_depths depth_count="10"> <block_depth level="0">26</block_depth> <block_depth level="1">23</block_depth> <block_depth level="2">48</block_depth> <block_depth level="3">31</block_depth> <block_depth level="4">13</block_depth> <block_depth level="5">6</block_depth> <block_depth level="6">0</block_depth> <block_depth level="7">0</block_depth> <block_depth level="8">0</block_depth> <block_depth level="9+">0</block_depth> </block_depths> </file> </files> </checkpoint> </checkpoints> </project> </sourcemonitor_metrics>
using linq2xml write query values.
see below.
var elements = xelement.parse(xml); var files = elements.element("project").element("checkpoints").element("checkpoint").element("files").elements("file").tolist(); var file_name = files[0].attribute("file_name"); var m5 = files[0].element("metrics").elements("metric").where(x => x.attribute("id").value.equals("m5")).first().value; var m6 = files[0].element("metrics").elements("metric").where(x => x.attribute("id").value.equals("m6")).first().value;
load xml string inside variable 'xml'
you'll notice i've bothered querying first item in enumerable ... of course can decide do.
Comments
Post a Comment