Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 05, 2011 @ 15:06
    Jeroen Breuer
    0

    Root node removed problem in DynamicXml

    Hello,

    In some situations I've got a problem if the root node is removed from DynamicXml. Here is an example where I use the Embedded Content package:

    The xml from umbraco.config:

    <projectUrls>
    <data>
    <item id="1">
    <url propertyid="1">http://www.project2.nl</url>;
    <title propertyid="2">Project 2</title>
    </item>
    <item id="2">
    <url propertyid="1">http://www.project3.nl</url>;
    <title propertyid="2">Project 3</title>
    </item>
    </data>
    </projectUrls>

    The razor code:

    foreach (dynamic d in Model.projectUrls.item)
    {
    string s1 = d.url;
    string s2 = d.title;
    }

    This code works if the item element is available 2 times. If I do a quickwatch on (Model.projectUrls.item).BaseElement I get this:

    <root>
    <item id="1">
    <url propertyid="1">http://www.project2.nl</url>;
    <title propertyid="2">Project 2</title>
    </item>
    <item id="2">
    <url propertyid="1">http://www.project3.nl</url>;
    <title propertyid="2">Project 3</title>
    </item>
    </root>

    Now if I remove an item this becomes my xml from umbraco.config

    <projectUrls>
    <data>
    <item id="1">
    <url propertyid="1">http://www.project2.nl</url>;
    <title propertyid="2">Project 2</title>
    </item>
    </data>
    </projectUrls>

    If I now try to run the same razor code as above I get this error: "umbraco.MacroEngines.DynamicXml' does not contain a definition for 'url'". If I do a quickwacth on (Model.projectUrls.item).BaseElement again I get this:

    <item id="1">
    <url propertyid="1">http://www.project2.nl</url>;
    <title propertyid="2">Project 2</title>
    </item>

    So if only 1 item is available the root node is removed again and the "d" value from the razor loop becomes the url element and thus I can't even reach the title element anymore in my loop. This is a serious problem and probably a bug.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 05, 2011 @ 15:10
    Jeroen Breuer
    0

    I've also created a workitem for this: http://umbraco.codeplex.com/workitem/30291

    Jeroen

  • Alex 78 posts 136 karma points
    May 05, 2011 @ 16:18
    Alex
    0

    Hi Jeroen,

     

    I think this is the same problem that is detailed in the topic

    http://our.umbraco.org/forum/developers/razor/18859-Question-for-an-XML-Data-Loop?p=1

    In the end we ended up having to check the Base Element within the foreach loop

    So I think your code would end up something like this

     

    foreach (dynamic d in Model.projectUrls)
    { if (d.BaseElement.Name == "item")
    {
    string s1 = d.url; string s2 = d.title; } else { string s1 = d.item.url; string s2 = d.item.title; }
    }

     

    Hope that helps

    Alex

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 05, 2011 @ 16:43
    Jeroen Breuer
    0

    Hello Alex,

    You sample probably works, but as I already said here I think this is too complicated and it should be easier. I think this is a bug and that's why I created the workitem.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 05, 2011 @ 16:49
    Jeroen Breuer
    0

    The easiest solution would to never remove the root node. I don't understand why the root node is removed anyway.

    Jeroen

  • Alex 78 posts 136 karma points
    May 05, 2011 @ 16:57
    Alex
    0

    Yes I agree, in fact I think I said the same in last post on the original topic I posted the link too.

    Hopefully the workitem you created will draw some attention to it.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 05, 2011 @ 17:56
    Jeroen Breuer
    0

    Hmm I just discoverd that this also works with both 1 item and 2 items:

    foreach (dynamic d in Model.projectUrls)
    {
       string s1 = d.url;
       
    string s2 = d.title;
       

    }

    Probably because the element returned in the foreach is <data> which is a single element and removed. The subelement is item which now works with 1 and more elements. Maybe I've got something here ;-).

    Jeroen

  • Toni Becker 146 posts 425 karma points
    May 05, 2011 @ 20:02
    Toni Becker
    0

    hy everybody longtime abstinence but now i'm back with a little bit of time and useful tips. during the last weeks and intense work with asp mvc3 in relation with razor, i discovered a lot of interesting stuff. The problem is not the removed root node. The logic behind is the dynamic node context, so if there are different values inside the element it will be casted as an dynamicxml e.g. node. So you have to understand the logic behind.

    I will show you an example tomorrow to understand what i mean. it's some troubleshooting and thinking behind to realize the nature of razor and the great abilities.

    So long and good evening to the rest of the community.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    May 06, 2011 @ 10:30
Please Sign in or register to post replies

Write your reply to:

Draft