Trey Copeland
Trey Copeland

Reputation: 3527

Referencing first child and getting object reference error

I have two different XML responses that can be returned. They look like this:

1) Gets returned if no user.

<ArrayUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

2: Gets returned if user found.

<ArrayUser xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <User>
    <Name></Name>
    <ID></ID>
  </User>

Here is my current code:

        if (userLookupResponse.DocumentElement.FirstChild.Name.Equals("User")) 
        {
            XmlNamespaceManager nsm = addXmlNamespaces(userLookupResponse);
            userLookupResponse.LoadXml(userLookupResponse.SelectSingleNode("//SSO:User", nsm).OuterXml);
            return userLookupResponse;
        }

My issue is that if a user isn't returned it errors on the if statement with an object reference not set to instance of an object error. How can I go inside the if statement only if number 2 is returned with user information?

Thanks.

Upvotes: 1

Views: 84

Answers (1)

Science_Fiction
Science_Fiction

Reputation: 3443

if (userLookupResponse.DocumentElement.FirstChild.Name.Equals("User"))

I am guessing that FirstChild will be null since there is no child.

Why not change your test abit to see:

if (userLookupResponse.DocumentElement.FirstChild != null)

Upvotes: 1

Related Questions