如何使用 c# 读取此代码覆盖率 xml 文件报告
Posted
技术标签:
【中文标题】如何使用 c# 读取此代码覆盖率 xml 文件报告【英文标题】:How to read this code coverage xml file report using c# 【发布时间】:2021-07-21 14:50:26 【问题描述】:读取这个xml文件获取程序集名称及其覆盖率小于95的,我需要读取代码覆盖率值小于95%的程序集名称,并存储该程序集对象中的名称和覆盖百分比。
<?xml version="1.0" encoding="UTF-8"?>
-<Root DotCoverVersion="2021.1.2" ReportType="Xml" CoveragePercent="97" TotalStatements="108" CoveredStatements="105">
-<Assembly CoveragePercent="94" TotalStatements="54" CoveredStatements="51" Name="ClassLibrary1">
-<Namespace CoveragePercent="94" TotalStatements="54" CoveredStatements="51" Name="ClassLibrary1">
-<Type CoveragePercent="94" TotalStatements="54" CoveredStatements="51" Name="Class1">
<Method CoveragePercent="100" TotalStatements="3" CoveredStatements="3" Name="Add(int,int):int"/>
<Method CoveragePercent="100" TotalStatements="16" CoveredStatements="16" Name="Armstrong(int):string"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Decision(int):int"/>
<Method CoveragePercent="79" TotalStatements="14" CoveredStatements="11" Name="isApplicable(int):string"/>
<Method CoveragePercent="100" TotalStatements="3" CoveredStatements="3" Name="sub(int,int):int"/>
<Method CoveragePercent="100" TotalStatements="13" CoveredStatements="13" Name="total(int):int"/>
</Type>
</Namespace>
</Assembly>
-<Assembly CoveragePercent="100" TotalStatements="54" CoveredStatements="54" Name="UnitTestProject2">
-<Namespace CoveragePercent="100" TotalStatements="54" CoveredStatements="54" Name="UnitTestProject2">
-<Type CoveragePercent="100" TotalStatements="54" CoveredStatements="54" Name="TestDemo">
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Add():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Armstrong_False():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Armstrong_InvalidNumber():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Armstrong_True():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Decision_False():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Decision_True():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_isApplicable_Applicable():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_isApplicable_NotValidAge():void"/>
<Method CoveragePercent="100" TotalStatements="5" CoveredStatements="5" Name="Test_Sub():void"/>
<Method CoveragePercent="100" TotalStatements="9" CoveredStatements="9" Name="Test_total():void"/>
</Type>
</Namespace>
</Assembly>
</Root>
这是xml文件。
【问题讨论】:
【参考方案1】:你可以试试下面的代码,使用XDocument
来实现小于95的程序集名称和覆盖率。
string path = "E:\\test.xml";
XDocument doc = XDocument.Load(path);
var result = doc.Descendants("Assembly").Where(i => Convert.ToInt32(i.Attribute("CoveragePercent").Value) < 95);
foreach (var item in result)
Console.WriteLine(item.Attribute("CoveragePercent").Value);
Console.WriteLine(item.Attribute("Name").Value);
结果:
【讨论】:
非常感谢 :)【参考方案2】:尝试以下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication
public class Program
const string FILENAME = @"c:\temp\test.xml";
public static void Main(string[] args)
XDocument doc = XDocument.Load(FILENAME);
XElement xRoot = doc.Root;
Root root = new Root()
DotCoverVersion = (string)xRoot.Attribute("DotCoverVersion"),
ReportType = (string)xRoot.Attribute("ReportType"),
CoveragePercent = (int)xRoot.Attribute("CoveragePercent"),
TotalStatements = (int)xRoot.Attribute("TotalStatements"),
CoveredStatements = (int)xRoot.Attribute("CoveredStatements"),
Level = 0
;
Node.GetNodes(xRoot, null, 1);
List<Node> lowCoverage = Root.Nodes.Where(x => x.CoveragePercent < 95).ToList();
foreach (Node node in lowCoverage)
Console.WriteLine("Name = 0, Percentage = 1", node.Name, node.CoveragePercent.ToString());
Console.ReadLine();
public class Root
public string DotCoverVersion get; set;
public string ReportType get; set;
public int CoveragePercent get; set;
public int TotalStatements get; set;
public int CoveredStatements get; set;
public static List<Node> Nodes = new List<Node>();
public int Level get; set;
public class Node: Root
public string Name get; set;
public string TagName get; set;
public Node ParentNode get; set;
public static void GetNodes(XElement xParent, Node parentNode, int level)
foreach (XElement child in xParent.Elements())
Node newNode = new Node();
Root.Nodes.Add(newNode);
newNode.Name = (string)child.Attribute("Name");
newNode.TagName = child.Name.LocalName;
newNode.ParentNode = parentNode;
newNode.Level = level;
newNode.TotalStatements = (int)child.Attribute("TotalStatements");
newNode.CoveragePercent = (int)child.Attribute("CoveragePercent");
newNode.CoveredStatements = (int)child.Attribute("CoveredStatements");
GetNodes(child, newNode, level + 1);
【讨论】:
以上是关于如何使用 c# 读取此代码覆盖率 xml 文件报告的主要内容,如果未能解决你的问题,请参考以下文章
如何在不覆盖以前数据的情况下将数据添加到 XML 文件(C#)