2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Linq ;
5
+ using System . Text ;
5
6
using System . Xml ;
7
+ using System . Xml . Linq ;
6
8
7
9
namespace Coverlet . Core . Reporters
8
10
{
@@ -16,82 +18,81 @@ public string Report(CoverageResult result)
16
18
{
17
19
CoverageSummary summary = new CoverageSummary ( ) ;
18
20
19
- XmlDocument xml = new XmlDocument ( ) ;
20
- XmlElement coverage = xml . CreateElement ( "coverage" ) ;
21
- coverage . SetAttribute ( "line-rate" , summary . CalculateLineCoverage ( result . Modules ) . ToString ( ) ) ;
22
- coverage . SetAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( result . Modules ) . ToString ( ) ) ;
23
- coverage . SetAttribute ( "version" , "1.9" ) ;
24
- coverage . SetAttribute ( "timestamp" , ( ( int ) ( DateTime . UtcNow - new DateTime ( 1970 , 1 , 1 ) ) . TotalSeconds ) . ToString ( ) ) ;
21
+ XDocument xml = new XDocument ( ) ;
22
+ XElement coverage = new XElement ( "coverage" ) ;
23
+ coverage . Add ( new XAttribute ( "line-rate" , summary . CalculateLineCoverage ( result . Modules ) . ToString ( ) ) ) ;
24
+ coverage . Add ( new XAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( result . Modules ) . ToString ( ) ) ) ;
25
+ coverage . Add ( new XAttribute ( "version" , "1.9" ) ) ;
26
+ coverage . Add ( new XAttribute ( "timestamp" , ( ( int ) ( DateTime . UtcNow - new DateTime ( 1970 , 1 , 1 ) ) . TotalSeconds ) . ToString ( ) ) ) ;
25
27
26
- XmlElement sources = xml . CreateElement ( "sources" ) ;
28
+ XElement sources = new XElement ( "sources" ) ;
27
29
foreach ( var src in GetSources ( result . Modules ) )
28
30
{
29
- XmlElement source = xml . CreateElement ( "source" ) ;
30
- source . AppendChild ( xml . CreateTextNode ( src ) ) ;
31
- sources . AppendChild ( source ) ;
31
+ XElement source = new XElement ( "source" , src ) ;
32
+ sources . Add ( source ) ;
32
33
}
33
34
34
- XmlElement packages = xml . CreateElement ( "packages" ) ;
35
+ XElement packages = new XElement ( "packages" ) ;
35
36
foreach ( var module in result . Modules )
36
37
{
37
- XmlElement package = xml . CreateElement ( "package" ) ;
38
- package . SetAttribute ( "line-rate" , summary . CalculateLineCoverage ( module . Value ) . ToString ( ) ) ;
39
- package . SetAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( module . Value ) . ToString ( ) ) ;
40
- package . SetAttribute ( "complexity" , "0" ) ;
38
+ XElement package = new XElement ( "package" ) ;
39
+ package . Add ( new XAttribute ( "line-rate" , summary . CalculateLineCoverage ( module . Value ) . ToString ( ) ) ) ;
40
+ package . Add ( new XAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( module . Value ) . ToString ( ) ) ) ;
41
+ package . Add ( new XAttribute ( "complexity" , "0" ) ) ;
41
42
42
- XmlElement classes = xml . CreateElement ( "classes" ) ;
43
+ XElement classes = new XElement ( "classes" ) ;
43
44
foreach ( var document in module . Value )
44
45
{
45
46
foreach ( var cls in document . Value )
46
47
{
47
- XmlElement @class = xml . CreateElement ( "class" ) ;
48
- @class . SetAttribute ( "name" , cls . Key ) ;
49
- @class . SetAttribute ( "filename" , Path . GetFileName ( document . Key ) ) ;
50
- @class . SetAttribute ( "line-rate" , summary . CalculateLineCoverage ( cls . Value ) . ToString ( ) ) ;
51
- @class . SetAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( cls . Value ) . ToString ( ) ) ;
52
- @class . SetAttribute ( "complexity" , "0" ) ;
53
-
54
- XmlElement methods = xml . CreateElement ( "methods" ) ;
48
+ XElement @class = new XElement ( "class" ) ;
49
+ @class . Add ( new XAttribute ( "name" , cls . Key ) ) ;
50
+ @class . Add ( new XAttribute ( "filename" , Path . GetFileName ( document . Key ) ) ) ;
51
+ @class . Add ( new XAttribute ( "line-rate" , summary . CalculateLineCoverage ( cls . Value ) . ToString ( ) ) ) ;
52
+ @class . Add ( new XAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( cls . Value ) . ToString ( ) ) ) ;
53
+ @class . Add ( new XAttribute ( "complexity" , "0" ) ) ;
54
+
55
+ XElement methods = new XElement ( "methods" ) ;
55
56
foreach ( var meth in cls . Value )
56
57
{
57
- XmlElement method = xml . CreateElement ( "method" ) ;
58
- method . SetAttribute ( "name" , meth . Key . Split ( ':' ) [ 2 ] . Split ( '(' ) [ 0 ] ) ;
59
- method . SetAttribute ( "signature" , meth . Key ) ;
60
- method . SetAttribute ( "line-rate" , summary . CalculateLineCoverage ( meth . Value ) . ToString ( ) ) ;
61
- method . SetAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( meth . Value ) . ToString ( ) ) ;
58
+ XElement method = new XElement ( "method" ) ;
59
+ method . Add ( new XAttribute ( "name" , meth . Key . Split ( ':' ) [ 2 ] . Split ( '(' ) [ 0 ] ) ) ;
60
+ method . Add ( new XAttribute ( "signature" , meth . Key ) ) ;
61
+ method . Add ( new XAttribute ( "line-rate" , summary . CalculateLineCoverage ( meth . Value ) . ToString ( ) ) ) ;
62
+ method . Add ( new XAttribute ( "branch-rate" , summary . CalculateBranchCoverage ( meth . Value ) . ToString ( ) ) ) ;
62
63
63
- XmlElement lines = xml . CreateElement ( "lines" ) ;
64
+ XElement lines = new XElement ( "lines" ) ;
64
65
foreach ( var ln in meth . Value )
65
66
{
66
- XmlElement line = xml . CreateElement ( "line" ) ;
67
- line . SetAttribute ( "number" , ln . Key . ToString ( ) ) ;
68
- line . SetAttribute ( "hits" , ln . Value . Hits . ToString ( ) ) ;
69
- line . SetAttribute ( "branch" , ln . Value . IsBranchPoint . ToString ( ) ) ;
67
+ XElement line = new XElement ( "line" ) ;
68
+ line . Add ( new XAttribute ( "number" , ln . Key . ToString ( ) ) ) ;
69
+ line . Add ( new XAttribute ( "hits" , ln . Value . Hits . ToString ( ) ) ) ;
70
+ line . Add ( new XAttribute ( "branch" , ln . Value . IsBranchPoint . ToString ( ) ) ) ;
70
71
71
- lines . AppendChild ( line ) ;
72
+ lines . Add ( line ) ;
72
73
}
73
74
74
- method . AppendChild ( lines ) ;
75
- methods . AppendChild ( method ) ;
75
+ method . Add ( lines ) ;
76
+ methods . Add ( method ) ;
76
77
}
77
78
78
- @class . AppendChild ( methods ) ;
79
- classes . AppendChild ( @class ) ;
79
+ @class . Add ( methods ) ;
80
+ classes . Add ( @class ) ;
80
81
}
81
82
}
82
83
83
- package . AppendChild ( classes ) ;
84
- packages . AppendChild ( package ) ;
84
+ package . Add ( classes ) ;
85
+ packages . Add ( package ) ;
85
86
}
86
87
87
- coverage . AppendChild ( sources ) ;
88
- coverage . AppendChild ( packages ) ;
89
- xml . AppendChild ( coverage ) ;
88
+ coverage . Add ( sources ) ;
89
+ coverage . Add ( packages ) ;
90
+ xml . Add ( coverage ) ;
90
91
91
- StringWriter writer = new StringWriter ( ) ;
92
- xml . Save ( writer ) ;
92
+ var stream = new MemoryStream ( ) ;
93
+ xml . Save ( stream ) ;
93
94
94
- return writer . ToString ( ) ;
95
+ return Encoding . UTF8 . GetString ( stream . ToArray ( ) ) ;
95
96
}
96
97
97
98
private string [ ] GetSources ( Modules modules )
0 commit comments