0

I have this XML file

<?xml version="1.0" encoding="utf-8"?>
<message>
 <success/>
  <bookings>
   <booking>
     <rooms>
      <room roomCode ="101" uniqueId="abc">
      <stays>
        <stay usedfrom="9:30" usedto="10:30" quantity="1" Price="62.5" rateCode="1"/>
      </stays>
      <extras>
        <extra from="9:30" to="10:30" unitPrice="5.5" extraCode="coffee" quantity="1" inclusive="0"/>
      </extras>
      <guests>
        <guest firstName="John" lastName="Doe" title="MR" ageRange="0"/>
      </guests>
    </room>
    <room roomCode ="Brd" uniqueId="xyz">
      <stays>
        <stay usedfrom="13:30" usedto="15:30" quantity="1" unitPrice="60.0000" rateCode="RACK"/>
      </stays>
      <guests>
        <guest firstName="Jean" lastName="Doe" title="MRS" ageRange="0"/>
      </guests>
    </room>
  </rooms>
</booking>

and i'm trying to run a check to make sure its in the correct format (ie certain elements are present). The code I have been using is

XmlNodeList Successful = doc.GetElementsByTagName("success");
XmlNodeList Bookings = doc.GetElementsByTagName("bookings");
XmlNodeList Booking = doc.GetElementsByTagName("booking");
XmlNodeList Rooms = doc.GetElementsByTagName("rooms");

if ((Successful != null) && (Bookings != null) && (Booking != null) && (Rooms != null))
{
    //do something
}
else
{
    //do something else
}

This ALWAYS works thought.

If I change the one of the values to

XmlNodeList Rooms = doc.GetElementsByTagName("NoSuchElement");

(which does not exist in the XML) it still "works".

Can someone point out what I've done wrong (I've tried removing the outer brackets from the "if" statement, but this did not change the outcome).

Thanks

5
  • When you say "This ALWAYS works though" do you mean it always executes the do something code? Commented Sep 6, 2017 at 14:34
  • 6
    By the way - an XmlNodeList will not be null even though it is empty. Have you tried checking the count of nodes in the list instead? Commented Sep 6, 2017 at 14:35
  • 1
    You are checking to see if the XmlNodeList is null, not if it contains any elements. if (Successful.Count > 0 && ...) Commented Sep 6, 2017 at 14:36
  • 6
    Literally solvable by using the debugger or the documentation. Let this be a lesson, eh? Commented Sep 6, 2017 at 14:38
  • @GeraldOakham a valuable lesson - in C# any collection object can be not null but also be empty! Commented Sep 6, 2017 at 15:02

2 Answers 2

4

As opf the docs:

An XmlNodeList containing a list of all matching nodes. If no nodes match name, the returned collection will be empty.

The method can´t return null, but an empty list. So check for this instead:

if(theNodeList.Any()) { ... }
else { /* error */ }
Sign up to request clarification or add additional context in comments.

Comments

3

This is because oc.GetElementsByTagName("NoSuchElement"); is never null.

As per the documentation, if no element exists, it returns en empty collection which is not null.

You have to check the Count of the result.

something like :

if (Successful.Count() != 0 && ...)

or

if (!Successful.Any() && ... )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.