Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Lennart Stoop 304 posts 842 karma points
    Jun 26, 2011 @ 13:23
    Lennart Stoop
    0

    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? :-)

     

  • Trevor Loader 199 posts 256 karma points
    Jun 27, 2011 @ 04:03
    Trevor Loader
    0

    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);

  • Lennart Stoop 304 posts 842 karma points
    Jun 27, 2011 @ 12:10
    Lennart Stoop
    0

    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 :-)

  • Lennart Stoop 304 posts 842 karma points
    Jun 28, 2011 @ 10:38
    Lennart Stoop
    1

    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>  
      }
    }

Please Sign in or register to post replies

Write your reply to:

Draft