I'm trying to assist @FarmFreshCode in writing a razor script which is to iterate through XML data (OpeningHours data type) stored in a member property. When accessing the property the XML data is returned as a string however and I wonder what would be the best way to convert this into something like DynamicNode or DynamicXml.
@using umbraco.cms.businesslogic.member @{ <table> @foreach(var member in Member.GetAll) { <tr> <td> @member.getProperty("openingHours").Value; </td> </tr> } </table> }
Yes, that would work although I'm not sure if it's best practice - I was wondering if Umbraco already provides such a helper method or maybe there's another way of doing it.. But yeah, creating a helper method in .NET should work, thanks :-)
Ok, here's what I got so far - pretty raw code but still learning.
In the script I create a DynamicXml and then iterate the data using Linq2Xml. On codeplex I noticed that @agrath has made several changes to DynamicXml which will be available in version 4.7.1 and it looks like these changes will make DynamicXml much easier to use and there won't be a need for Linq2Xml :-)
@using umbraco.cms.businesslogic.member @using umbraco.MacroEngines @using System.Xml.Linq @using TheseDays.Umbraco.DataTypes.OpeningHours @{ var member = new Member(1094); if (member.getProperty("openingHours") != null) { DynamicXml openingHours = new DynamicXml( XElement.Parse(member.getProperty("openingHours").Value.ToString())); <table> @foreach (var item in openingHours.BaseElement.Elements("scheduleItem")) { <tr> <td>@HelperMethods.LocalizeWeekday(item.Element("weekDay").Value)</td> @if (item.Element("isScheduled").Value == "true") { var firstSet = item.Element("firstSet"); if (firstSet.Element("isValid").Value == "true") { <td>@HelperMethods.ConvertTo12HourClock(firstSet.Element("hourStart").Value)</td> <td>-</td> <td>@HelperMethods.ConvertTo12HourClock(firstSet.Element("hourEnd").Value)</td> } else { <td colspan="3"></td> } var secondSet = item.Element("secondSet"); if (openingHours.BaseElement.Element("hasSecondSet").Value == "true" && secondSet.Element("isValid").Value == "true") { <td>@HelperMethods.ConvertTo12HourClock(secondSet.Element("hourStart").Value)</td> <td>-</td> <td>@HelperMethods.ConvertTo12HourClock(secondSet.Element("hourEnd").Value)</td> } else { <td colspan="3"></td> } } else { <td colspan="6">Not scheduled</td> } </tr> } </table> } }
Working with XML in member properties
Hi all,
I'm trying to assist @FarmFreshCode in writing a razor script which is to iterate through XML data (OpeningHours data type) stored in a member property. When accessing the property the XML data is returned as a string however and I wonder what would be the best way to convert this into something like DynamicNode or DynamicXml.
@using umbraco.cms.businesslogic.member
@{
<table>
@foreach(var member in Member.GetAll) {
<tr>
<td>
@member.getProperty("openingHours").Value;
</td>
</tr>
}
</table>
}
Any ideas? :-)
Could you make a razor helper function and use normal .net functionality to convert the string to XML and return that as a dynamicNode?
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(rawXml);
Hi Trevor,
Yes, that would work although I'm not sure if it's best practice - I was wondering if Umbraco already provides such a helper method or maybe there's another way of doing it.. But yeah, creating a helper method in .NET should work, thanks :-)
Ok, here's what I got so far - pretty raw code but still learning.
In the script I create a DynamicXml and then iterate the data using Linq2Xml. On codeplex I noticed that @agrath has made several changes to DynamicXml which will be available in version 4.7.1 and it looks like these changes will make DynamicXml much easier to use and there won't be a need for Linq2Xml :-)
@using umbraco.cms.businesslogic.member
@using umbraco.MacroEngines
@using System.Xml.Linq
@using TheseDays.Umbraco.DataTypes.OpeningHours
@{
var member = new Member(1094);
if (member.getProperty("openingHours") != null) {
DynamicXml openingHours = new DynamicXml(
XElement.Parse(member.getProperty("openingHours").Value.ToString()));
<table>
@foreach (var item in openingHours.BaseElement.Elements("scheduleItem")) {
<tr>
<td>@HelperMethods.LocalizeWeekday(item.Element("weekDay").Value)</td>
@if (item.Element("isScheduled").Value == "true") {
var firstSet = item.Element("firstSet");
if (firstSet.Element("isValid").Value == "true") {
<td>@HelperMethods.ConvertTo12HourClock(firstSet.Element("hourStart").Value)</td>
<td>-</td>
<td>@HelperMethods.ConvertTo12HourClock(firstSet.Element("hourEnd").Value)</td>
} else {
<td colspan="3"></td>
}
var secondSet = item.Element("secondSet");
if (openingHours.BaseElement.Element("hasSecondSet").Value == "true"
&& secondSet.Element("isValid").Value == "true") {
<td>@HelperMethods.ConvertTo12HourClock(secondSet.Element("hourStart").Value)</td>
<td>-</td>
<td>@HelperMethods.ConvertTo12HourClock(secondSet.Element("hourEnd").Value)</td>
} else {
<td colspan="3"></td>
}
} else {
<td colspan="6">Not scheduled</td>
}
</tr>
}
</table>
}
}
is working on a reply...