解析XML - LINQ to XML或任何其他方法

时间:2014-03-06 14:09:38

标签: c# xml parsing

我需要提取“file_name”属性的值以及值为“M5”&的属性的innertext。 “M6”。基本上它是文件的名称以及它中存在的类和方法的数量。任何解析方法都适合我。我尝试了一些方法,我可以提取file_name但无法进一步获取指标ID ..请帮助

<?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 with Comments</metric_name>
      <metric_name id="M5" type="number">Classes and 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 Most Complex Method</metric_name>
      <metric_name id="M9" type="string">Name of Most 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>

2 个答案:

答案 0 :(得分:0)

除了调用Elemnt/s方法(检索元素的子元素)和Descendants方法(检索元素的所有后代)以沿着元素路径前进之外,当你知道确切的路径时,你可以通过XPath(通过在代码的头部添加using指令using System.Xml.XPath;)来检索特定的节点(元素/属性)。

语法很简单,您可以从wikipediaw3schools

了解它

答案 1 :(得分:0)

使用Linq2XML,您可以编写查询来获取值。

见下文。

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;

从变量'xml'

中的字符串加载xml

你会注意到我只是在查询“可枚举”中的第一项...当然你可以决定该怎么做。