我遇到一个问题,我无法从HttpWebResponse中获取每个元素值来填充正确的列。就是你知道的,过去一周我一直在网上搜索这个问题,尝试了很多不同的方法,它们以同样的方式结束。
我的输入流看起来像:
<results>
<result inum="848023000000101">
<field name="inum">848023000000101</field>
<field name="switchcallid">00001001011520423332</field>
<field name="startedat">2018-03-07T14:02:16.636</field>
<field name="duration">36</field>
<field name="otherparties">1320 (Johan R), 1322 (Craig Smythe)</field>
</result>
<result inum="848023000000103">
<field name="inum">848023000000103</field>
<field name="switchcallid">00001001941520500491</field>
<field name="startedat">2018-03-08T11:28:15.694</field>
<field name="duration">25</field>
<field name="otherparties">1323 (Barry Celliers), 1320 (Johan R)</field>
</result>
</results>
我的控制器代码如下:
using (HttpWebResponse response = APIrequestStream.GetResponse() as HttpWebResponse)
{ var file = XDocument.Load(response.GetResponseStream());
var descendants = file.Descendants();
foreach (var descendant in descendants)
{
//loop through each elements of descendant
foreach (var element in descendant.Elements().Elements())
{
List<Tuple<string>> Headers = new List<Tuple<string>>();
//assiging of element name as propertyName
string propertyName = element.FirstAttribute.Value;
Headers.Add(Tuple.Create(propertyName));
}
foreach (var element in descendant.Elements().Elements())
{
List<Tuple<string>> Rows = new List<Tuple<string>>();
string PropertyValue = element.Value;
datatbl.Rows.Add(PropertyValue);
}
}
我的观点如下:
<table class="table table-bordered table-striped">
<tr>
<th style="font-size:40px; font-weight:bold; text-align:center">iNUM</th>
<th style="font-size:40px; font-weight:bold; text-align:center">UCID</th>
<th style="font-size:40px; font-weight:bold; text-align:center">Start Date</th>
<th style="font-size:40px; font-weight:bold; text-align:center">Duration</th>
<th style="font-size:40px; font-weight:bold; text-align:center">Participants</th>
</tr>
@for (int i = 0; i < Model.Rows.Count; i++)
{ <tr>
<td style="font-size:100px; font-weight:bold; text-align:center;">@Model.Rows[i][0]</td>
<td style="font-size:100px; font-weight:bold; text-align:center;">@Model.Rows[i][1]</td>
<td style="font-size:100px; font-weight:bold; text-align:center;">@Model.Rows[i][2]</td>
<td style="font-size:100px; font-weight:bold; text-align:center;">@Model.Rows[i][3]</td>
<td style="font-size:100px; font-weight:bold; text-align:center;">@Model.Rows[i][4]</td>
</tr>
}
</table>
我的结果在页面上显示如下:
我需要格式化输出,以便每个值都在它自己的列中。 请有人协助或指导我找到一个好的解决方案。
答案 0 :(得分:0)
你做的事太复杂了。
var file = XDocument.Load(response.GetResponseStream());
foreach (var result in file.Root.Elements())
{
var values = result
.Elements() // field
.Select(e => e.Value)
.ToArray();
datatbl.Rows.Add(values);
}