with the help of the Stack overflow community I have been able to take some data from an XML document and add it to a list using the following code.
String URLString = "https://api3.libcal.com/api_hours_grid.php?iid=4246&format=xml&weeks=1&systemTime=0";
XDocument xdoc = new XDocument();
xdoc = XDocument.Load(URLString);
var location = (from p in xdoc.Descendants("location")
from s in p.Descendants("week").Elements()
select new
{
CampusName = (string)p.Element("name"),
WeekD = (string)s.Element("date"),
OpenHours = (string)s.Element("rendered"),
Day = s.Name.LocalName
}).ToList();
this works great, and I can output to an unordered list with the following
string currentLocation = "";
foreach (var item in location)
{
if (item.CampusName != currentLocation)
{
<h2>@item.CampusName</h2>
currentLocation = @item.CampusName;
}
<ul>
<li>@item.WeekD - @item.OpenHours</li>
</ul>
}
however, after reviewing my requirements I feel the data would be better displayed using a HTML Table. I have tried a few different ways using String builder to achieve this
string dayStop = "Sunday";
StringBuilder sb = new StringBuilder();
sb.Append("<TABLE>\n");
sb.Append("<TR>\n");
foreach (var item in location)
{
if (item.Day != dayStop)
{
sb.Append("<TH>\n");
sb.Append(@item.Day);
sb.Append("</TH>\n");
//dayStop = @item.Day;
}
}
sb.Append("</TR>\n");
sb.Append("<TR>\n");
sb.Append("<TD>\n");
sb.Append(@item.CampusName);
sb.Append("</TD>\n");
sb.Append("<TD>\n");
sb.Append(@item.OpenHours);
sb.Append("</TD>\n");
sb.Append("</TR>\n");
sb.Append("</TABLE > ");
}
Html.Raw(sb)
but its not working. I'd like the value of @item.Day to populate the value of TH, but as each item in my list contains value day value for each @item.CampusName I end up with a whole bunch of unnecessary @item.Day values and I feel like foreach is not the right answer here. What I'd like to end up with is something like the following snippet;
<table>
<tr>
<th>
Location
</th>
<th>
Sunday
</th>
<th>
Monday
</th>
<th>
Tueday
</th>
</tr>
<tr>
<td>
Campus 1
</td>
<td>
9 - 5
</td>
<td>
9 - 5
</td>
<td>
9 - 5
</td>
</tr>
<tr>
<td>
Campus 2
</td>
<td>
9 - 5
</td>
<td>
9 - 5
</td>
<td>
9 - 5
</td>
</tr>
</table>