Copied to clipboard

Flag this post as spam?

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


  • Michael Lawrence 128 posts 200 karma points
    Mar 01, 2011 @ 21:08
    Michael Lawrence
    0

    How to iterate DynamicXml?

    Basically, I'm not quite sure how I am supposted to iterate through nodes in the DynamicXml class.

    Here is my xml:

    <data>
        <item id="1">
            <question propertyid="1">Question 1</question>
            <answer propertyid="2"><![CDATA[Answer 1]]></answer>
        </item>
        <item id="2">
            <question propertyid="1">Question 2</question>
            <answer propertyid="2"><![CDATA[Answer 2]]></answer>
        </item>
        <item id="3">
            <question propertyid="1">Question 3</question>
            <answer propertyid="2"><![CDATA[Answer 3]]></answer>
        </item>
    </data>
    
    Here is my code:

    <umbraco:Macro runat="server" language="razor">
    @using System
    @using System.Linq
    @using System.Collections.Generic
    @using umbraco.MacroEngines
          
    @foreach(DynamicXml item in Model.questions.item) {
      @item } </umbraco:Macro>

    And here is the error:

    Error loading Razor Script Cannot implicitly convert type 
    'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'. 
    An explicit conversion exists (are you missing a cast?)

    I've even tried doing a "@foreach(System.Collections.IEnumerable item in Model.questions.item)..." and it get the same error. What am I doing wrong?

  • Michael Lawrence 128 posts 200 karma points
    Mar 01, 2011 @ 21:20
    Michael Lawrence
    0

    Actually, I figured out a way:

    @foreach(XElement item in Model.questions.BaseElement.Elements("item")) {
    <h3>@item.Element("question").Value</h3>
    @Html.Raw(item.Element("answer").Value)
    }

    Is there a cleaner way to do this, or is this the preferred method?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Mar 02, 2011 @ 18:00
    Ismail Mayat
    0

    try Model.Questions.Item i think case is changed event though your doctype alias and property alias is low case.

    Regards

    Ismail

  • Michael Lawrence 128 posts 200 karma points
    Mar 02, 2011 @ 21:33
    Michael Lawrence
    0

    Model.Questions.Item doesn't work. It says that DynamicXml does not contain a definition for Item. Lowercase works, it's just cast as a DynamicXml class which is not IEnumerable. I see how this syntax would be useful for getting a single node in the xml, but still have no idea how to use it to enumerate across the nodes.

  • Gerty Engrie 130 posts 490 karma points c-trib
    Mar 03, 2011 @ 09:39
    Gerty Engrie
    0

    Same here, i need it to loop across the values in a uComponents: Multi-Node Tree Picker which produces the following xml:

        <serviceNavigation>
    <MultiNodePicker>
    <nodeId>1089</nodeId>
    <nodeId>1090</nodeId>
    </MultiNodePicker>
    </serviceNavigation>

    My razor file currently looks like this:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;

    <nav id="service-nav-wrap">
      <ul id="service-nav">

       
    @foreach(DynamicNode item in Model.AncestorOrSelf("site").serviceNavigation.nodeId)
        {
              <li>@item</li>
        }

     
    </ul>
    </nav>

    Cannot implicitly convert type 'umbraco.MacroEngines.DynamicXml' to 'System.Collections.IEnumerable'. An explicit conversion exists (are you missing a cast?)

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 03, 2011 @ 11:22
    Sebastiaan Janssen
    0

    Gareth is looking into it, for now, this one works for me:

    @using System.Xml.Linq;
    
    <ul>
    @foreach (XElement item in Model.MultiNodePicker.BaseElement.Elements("nodeId"))
    {
        <li>@item.Value</li>
    } 
    </ul>
  • lucuma 261 posts 563 karma points
    Mar 05, 2011 @ 01:31
    lucuma
    0

    Just curious if there is any news on this.  @Sebastiaan, your code does not work for me in my project (4.7RC - object does not contain a def for element).   I'm realling having a hard time pulling data out of the mnp to validate that the current page exists inside a mnp from another document.

     

    var sectors = Model.Parent.Parent.DescendantsOrSelf("Sector");
    foreach (var sector in sectors) 
    {
        foreach (XElement item in sector.MultiNodePicker.BaseElement.Elements("nodeId"))
        {
            <span>@item.Value</span>
        }
    }

     

  • Gerty Engrie 130 posts 490 karma points c-trib
    Mar 05, 2011 @ 07:47
    Gerty Engrie
    0

    Lucuma, try leaving out "MultiNodePicker", should work :)

  • lucuma 261 posts 563 karma points
    Mar 05, 2011 @ 16:39
    lucuma
    0

    I've tried that and about every other combination.  This is what I get without the MultiNodePicker:

    Here is my code:

     foreach (XElement item in sector.BaseElement.Elements("nodeId"))
                            {
                                @item.Value
                            }

    Here is the error message.  All sectors in this case have a multi node picker called projects.  All I'm trying to do is determine if the current page is contained inside the other page's mnp.  In other cases I need to loop over them and get the nodeid's to display information about that document.   Going crazy!

    Error loading Razor Script 
    'umbraco.MacroEngines.DynamicNull' does not contain a definition for 'Elements'
  • Gerty Engrie 130 posts 490 karma points c-trib
    Mar 05, 2011 @ 20:30
    Gerty Engrie
    2

    lucuma and now just put your property in between, because now you're doing that on your page element :)

    I ended up with the following code for my mutlinodepicker, but yours should work fine too if you put your propertyalias between "sector" and "BaseElement" :)

    @{   
    int i =1;
    var items = XDocument.Parse( new DynamicNode(Model.Id).AncestorOrSelf("site").GetProperty("serviceNavigation").Value).Descendants().Where(n => n.Name == "nodeId");

    int arrLength = items.Count();
    }

    @foreach (var item in items)
    {
    var Node = @Model.NodeById(@item.Value);
    string css = "";
    if (i == arrLength)
    { css = "last-item"; }
    <li class="@css"><a href="@Node.Url" title="@Node.Name">@Node.Name</a></li>

    i++;
    }
  • lucuma 261 posts 563 karma points
    Mar 05, 2011 @ 23:28
    lucuma
    0

    @Gerty - 

    I appreciate the help.  The failures were happening b/c I needed to check to make sure that MNP had a value before looping over it or you get BaseElement runtime errors.  Your code helped in that I am checking sector.GetProperty("projects").Value to make sure it isn't empty before looping.  Thanks for your help.

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 16, 2011 @ 14:05
    Hendy Racher
    0

    Hi, btw, if you're using uComponents and want to get the collection of nodes picked by a the MultiNodeTreePicker or the XPathCheckBoxList, you can also use:

    List<Node> pickedNodes = uQuery.GetNodesByXml(xmlString);

    HTH,

    Hendy

  • Johan Plesner Hamann 105 posts 199 karma points
    Jul 09, 2011 @ 19:11
    Johan Plesner Hamann
    0

    Do you want it simple? 
    Got this from Jedi-Master Dirk De Grave

       @foreach (var in Model.refKursus
            {
                  var @Model.NodeById(@x.InnerText);
                  <em class="date">@n.StartDato @n.slutDato</em>
            }
    @x.InnerText

    our.umbraco.org/..../21753-@foreach-value-of-Multiple-Textstring-uComponents-

    /Johan

Please Sign in or register to post replies

Write your reply to:

Draft