Copied to clipboard

Flag this post as spam?

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


  • Tom 713 posts 954 karma points
    Jul 19, 2011 @ 09:27
    Tom
    0

    XPath Returns no nodes error

    Is there a way to get razor to not blow up and instead just let you proceed??

     

    i.e.

    var collectionsWithProducts = Model.XPath("./productRelease [count(./product) > 0]");

    and then check for something??

  • Tom 713 posts 954 karma points
    Jul 19, 2011 @ 09:49
    Tom
    0

    i also tried returning a count like so:

     var collectionsWithProductsCount = Model.XPath("count(./productRelease [count(./product) > 0])");
          dynamic activeCollection = null;
        
          if(collectionsWithProductsCount > 0)
          {
            activeCollection = Model.XPath("./productRelease [count(./product) > 0]").FirstOrDefault();
          }

          if(activeCollection != null)
          {

     

    but alas

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 19, 2011 @ 10:59
    Sebastiaan Janssen
    1

    Don't use XPath selectors would be my answer.. but yeah, this should probably be filed as a bug in codeplex.

    For now you could either just use XSLT (why use Razor when all you're doing is XPath selections?) or do something like this in Razor:

            var activeCollection = new List<dynamic>();
            @foreach(var item in Model.Children.Where("nodeTypeAlias == \"productRelease\""))
            {
                if(item.Children.Where("nodeTypeAlias == \"product\"").Count() > 1)
                {
                    activeCollection.Add(item);
                }
            }
  • Tom 713 posts 954 karma points
    Jul 20, 2011 @ 00:38
    Tom
    0

    A great answer thank you..

    sorry still jsut feeling that out! but yes it would be great if they could fix that.. having a pile of exceptions being thrown isn't really a result you'd expect!

    I am using razor to learn it but in this instance I thought it'd be cleaner to get the results of the query in a single line of code rather than the approach shown but if that's how it needs to be done! that's how it needs to be done!

     

    Cheers,

    Tom

Please Sign in or register to post replies

Write your reply to:

Draft