Copied to clipboard

Flag this post as spam?

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


  • Aximili 177 posts 278 karma points
    Jun 02, 2015 @ 12:52
    Aximili
    0

    GetNodeByXpath throws NullReferenceException

    I'm working on a project using Umbraco 6 and WebForms.

    In Content, there are Employees. So umbraco.config contains these lines:

      <Employee id="15853" ...>
        <email>xxx</email>
      </Employee>

    In C#, how do I get the Employee node having email=xxx? I tried this:

    Node nodeEmail = Node.GetNodeByXpath("//Member/email[text()='xxx']");

    and it throws this exception:

    [NullReferenceException: Object reference not set to an instance of an object.]
       umbraco.NodeFactory.Node.initialize() +172
       umbraco.NodeFactory.Node..ctor(XmlNode NodeXmlNode) +179
       umbraco.NodeFactory.Node.GetNodeByXpath(String xpath) +168
  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 02, 2015 @ 12:56
    Chriztian Steinmeier
    0

    Hi Hardi,

    In the XPath expression you need to use the document type's alias (i.e. "Employee") - so if it's <Employee> in the XML, it should be Employee in the expression, i.e.:

    Node nodeEmail = Node.GetNodeByXpath("//Employee/email[. = 'xxx']");
    

    /Chriztian

  • Aximili 177 posts 278 karma points
    Jun 02, 2015 @ 15:20
    Aximili
    0

    Oh I'm sorry I cut and pasted the wrong thing. I can't edit it anymore.

    It was already Employee. But it throws a NullReferenceException.

    These lines are fine:

    Node nodeEmail = Node.GetNodeByXpath("//Employees");
    Node nodeEmail = Node.GetNodeByXpath("//Employees/Employee");
    Node nodeEmail = Node.GetNodeByXpath("//Employee");

    But this throws NullReferenceException

    Node nodeEmail = Node.GetNodeByXpath("//Employees/Employee/email");

     

  • Sebastian Dammark 583 posts 1407 karma points
    Jun 02, 2015 @ 18:52
    Sebastian Dammark
    2

    Try this:

    Node nodeEmail = Node.GetNodeByXpath("//Employee[email = 'xxx']");

     

  • Aximili 177 posts 278 karma points
    Jun 03, 2015 @ 14:08
    Aximili
    0

    It works! Thank you Sebastian!

    I still don't understand why the other queries were throwing the exception, even though they were fine when tested on XPath testers. But thanks!

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Jun 03, 2015 @ 14:17
    Chriztian Steinmeier
    1

    Hi Hardi,

    The error you get is because the code that deals with an umbraco Node, and all of the working XPath expressions return a node - the ones throwing an error, selects a property instead. I didn't catch that your code was using a node (the "nodeEmail" name threw me off, I guess :-). Sebastian's XPath selects the <Employee> node that has an <email> equal to 'xxx', whereas mine selected the actual <email> element. Subtle difference - but if your C# code expects a Node, the error message makes perfect sense.

    Does that help understanding?

    /Chriztian

  • Aximili 177 posts 278 karma points
    Jun 04, 2015 @ 01:47
    Aximili
    0

    Ah of course! I was concentrating too much on the XML I forgot about the Node itself! Thank you Chriztian :)

Please Sign in or register to post replies

Write your reply to:

Draft