Copied to clipboard

Flag this post as spam?

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


  • João Pereira 41 posts 64 karma points
    Jan 23, 2012 @ 14:58
    João Pereira
    0

    How to get attribute from current Xml Node

    Hi there,

    I'm trying to get the value of an attribute in a Xml Node using razor. The node in question is from blog4umbraco and it's all the comments from a post.

    My code looks like this:

    var postComments = (@BlogLibrary.GetCommentsForPost(@Model.Id));
    var allComments = @Library.ToDynamicXml(postComments);
    foreach (var comment in @allComments)
        {
            @Html.Raw(comment.name);
            @Html.Raw(comment.email);
            @Html.Raw(comment.website);
            @Html.Raw(comment.message);                    
        }

    The XML content of 'comment' is:

    <comment id="1" nodeid="1139" created="22-01-2012 19:44:25">
      <name>Joao</name>
      <email>[email protected]</email>
      <website>http://www.com.com</website>;
      <message>Bla bla bla bla bla</message>
    </comment>

    I can get the value from all inner nodes, but how do I get the value from the attributes of the 'comment' node such as 'created'?

    Thanks in advance!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 23, 2012 @ 15:07
    Jeroen Breuer
    0

    Have you tried this?

    @comment.created;

    Think since 4.7.1 that should work.

    Jeroen

  • João Pereira 41 posts 64 karma points
    Jan 23, 2012 @ 15:14
    João Pereira
    0

    Yes...

    That's the . notation referred in the Razor blog posts part 6.

    But that doesn't work... It says it doesn't exist...

    'umbraco.MacroEngines.DynamicXml' does not contain a definition for 'created'

    Edit: I'm using 4.7.1

    Edit2: Oh man... Thanks... I'm pretty sure I already tried that... After all I read that part... But for some reason it works now... 8-|

Please Sign in or register to post replies

Write your reply to:

Draft