想只保留 MR.RSRP 、 MR.RSRQ 部分,并且合并成一行; MR.Tadv 等其他节点内容不处理。 原始 xml
<bulkpmmrdatafile>
<fileheader fileformatversion="V1.0.4" reporttime="2016-08-19T15:15:00.000" starttime="2016-08-19T15:00:00.000" endtime="2016-08-19T15:15:00.000" period="15"></fileheader>
<enb id="33331">
<measurement mrname="MR.RSRP">
<smr>MR.RSRP.00 MR.RSRP.01 MR.RSRP.02 MR.RSRP.03 MR.RSRP.04 MR.RSRP.05 MR.RSRP.06 MR.RSRP.07 MR.RSRP.08 MR.RSRP.09 MR.RSRP.10 MR.RSRP.11 MR.RSRP.12 MR.RSRP.13 MR.RSRP.14 MR.RSRP.15 MR.RSRP.16 MR.RSRP.17 MR.RSRP.18 MR.RSRP.19 MR.RSRP.20 MR.RSRP.21 MR.RSRP.22 MR.RSRP.23 MR.RSRP.24 MR.RSRP.25 MR.RSRP.26 MR.RSRP.27 MR.RSRP.28 MR.RSRP.29 MR.RSRP.30 MR.RSRP.31 MR.RSRP.32 MR.RSRP.33 MR.RSRP.34 MR.RSRP.35 MR.RSRP.36 MR.RSRP.37 MR.RSRP.38 MR.RSRP.39 MR.RSRP.40 MR.RSRP.41 MR.RSRP.42 MR.RSRP.43 MR.RSRP.44 MR.RSRP.45 MR.RSRP.46 MR.RSRP.47 </smr>
<object id="8532747">
<v>6 68 33 30 54 45 35 53 43 34 23 26 29 47 61 73 69 59 82 77 87 79 85 62 44 38 33 40 49 30 40 31 18 14 8 6 21 10 2 2 0 0 1 0 0 0 0 1</v>
</object>
<object id="8532757">
<v>82 103 48 39 57 102 90 45 51 67 72 74 106 75 108 107 154 84 55 32 42 67 38 31 26 44 18 17 43 11 20 18 14 15 9 10 18 24 21 9 8 8 6 2 1 0 1 0</v>
</object>
<object id="8532767">
<v>94 248 64 87 90 67 71 72 84 80 78 87 83 137 118 123 141 146 151 151 142 108 110 102 115 115 168 133 134 124 94 130 121 73 98 51 67 111 65 46 29 9 5 1 1 0 0 0</v>
</object>
</measurement>
<measurement mrname="MR.RSRQ">
<smr>MR.RSRQ.00 MR.RSRQ.01 MR.RSRQ.02 MR.RSRQ.03 MR.RSRQ.04 MR.RSRQ.05 MR.RSRQ.06 MR.RSRQ.07 MR.RSRQ.08 MR.RSRQ.09 MR.RSRQ.10 MR.RSRQ.11 MR.RSRQ.12 MR.RSRQ.13 MR.RSRQ.14 MR.RSRQ.15 MR.RSRQ.16 MR.RSRQ.17 </smr>
<object id="8532747">
<v>1 2 1 2 4 17 27 62 121 202 328 232 208 304 133 3 0 1</v>
</object>
<object id="8532757">
<v>2 0 4 8 14 30 66 172 201 224 328 443 264 228 84 3 1 0</v>
</object>
<object id="8532767">
<v>1 1 4 8 21 50 101 205 346 501 594 557 556 929 430 16 2 2</v>
</object>
</measurement>
<measurement mrname="MR.Tadv">
<smr>MR.Tadv.00 MR.Tadv.01 MR.Tadv.02 MR.Tadv.03 MR.Tadv.04 MR.Tadv.05 MR.Tadv.06 MR.Tadv.07 MR.Tadv.08 MR.Tadv.09 MR.Tadv.10 MR.Tadv.11 MR.Tadv.12 MR.Tadv.13 MR.Tadv.14 MR.Tadv.15 MR.Tadv.16 MR.Tadv.17 MR.Tadv.18 MR.Tadv.19 MR.Tadv.20 MR.Tadv.21 MR.Tadv.22 MR.Tadv.23 MR.Tadv.24 MR.Tadv.25 MR.Tadv.26 MR.Tadv.27 MR.Tadv.28 MR.Tadv.29 MR.Tadv.30 MR.Tadv.31 MR.Tadv.32 MR.Tadv.33 MR.Tadv.34 MR.Tadv.35 MR.Tadv.36 MR.Tadv.37 MR.Tadv.38 MR.Tadv.39 MR.Tadv.40 MR.Tadv.41 MR.Tadv.42 MR.Tadv.43 MR.Tadv.44 </smr>
<object id="8532747">
<v>3 25 83 338 288 437 206 89 70 45 33 17 11 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</v>
</object>
<object id="8532757">
<v>10 18 178 357 254 238 127 25 43 153 204 127 267 54 3 0 0 2 11 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</v>
</object>
<object id="8532767">
<v>8 56 341 883 956 641 399 290 407 183 62 20 7 0 0 13 57 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0</v>
</object>
</measurement>
<measurement mrname="MR.PowerHeadRoom">
</measurement>
</enb>
</bulkpmmrdatafile>
目标格式
2016-08-19 15:15:00,2016-08-19 15:00:00,2016-08-19 15:15:00,15,33331,8532747,6,68,33,30,54,45,35,53,43,34,23,26,29,47,61,73,69,59,82,77,87,79,85,62,44,38,33,40,49,30,40,31,18,14,8,6,21,10,2,2,0,0,1,0,0,0,0,1,1,2,1,2,4,17,27,62,121,202,328,232,208,304,133,3,0,1
2016-08-19 15:15:00,2016-08-19 15:00:00,2016-08-19 15:15:00,15,33331,8532757,82,103,48,39,57,102,90,45,51,67,72,74,106,75,108,107,154,84,55,32,42,67,38,31,26,44,18,17,43,11,20,18,14,15,9,10,18,24,21,9,8,8,6,2,1,0,1,0,2,0,4,8,14,30,66,172,201,224,328,443,264,228,84,3,1,0
2016-08-19 15:15:00,2016-08-19 15:00:00,2016-08-19 15:15:00,15,33331,8532767,94,248,64,87,90,67,71,72,84,80,78,87,83,137,118,123,141,146,151,151,142,108,110,102,115,115,168,133,134,124,94,130,121,73,98,51,67,111,65,46,29,9,5,1,1,0,0,0,1,1,4,8,21,50,101,205,346,501,594,557,556,929,430,16,2,2
目前 measurement 里面的内容过滤和转换都失败了。
void XmlToCsv(string xmlPath)
{
XElement xeMrs = XElement.Load(xmlPath);
string fileHeader = (from el in xeMrs.Elements("fileHeader")
select String.Format("{0},{1},{2},{3}",
(string)el.Attribute("reportTime").Value.Trim().Substring(0, 19).Replace("T", " "),
(string)el.Attribute("startTime").Value.Trim().Substring(0, 19).Replace("T", " "),
(string)el.Attribute("endTime").Value.Trim().Substring(0, 19).Replace("T", " "),
(string)el.Attribute("period"))).Aggregate(
new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString());
string eNb = xeMrs.Element("eNB").Attribute("id").Value;
// 转换失败,
string smr = (from el in xeMrs.Element("eNB").Elements("measurement").Elements("object")
//where (string)xeMrs.Element("eNB").Element("measurement").Attribute("mrName") == ("MR.RSRQ")
select String.Format("{0},{1},{2}",
(string)el.Attribute("id"),
(string)el.Element("v"),
Environment.NewLine)).Aggregate(
new StringBuilder(), (sb, s) => sb.Append(s), sb => sb.ToString());
this.textBoxLog.AppendText(smr + Environment.NewLine);
}