Reputation: 3702
I am trying to figure out if I have an XmlNodeList object, and Count is greater or equal to 1, will its "Item" object ever be null?
If so, how can I check if it is null before calling its "HasChildNode" property?
if (XmlNodeList.Item(0).HasChildNodes)
Thanks,
Upvotes: 1
Views: 3124
Reputation: 41
It is not null even if there is no node inside the XmlNodeList! You may try "Count" method to check:
XmlNodeList TheXmlNodeList = GetMenuItems();
if (TheXmlNodeList.Count > 0)
{
//has node
}
else
{
//do not have node
}
Upvotes: 1
Reputation: 43046
to answer the (contrafactual?) "if so" question:
if (XmlNodeList.Item(0) != null && XmlNodeList.Item(0).HasChildNodes)
Upvotes: 0
Reputation: 160922
No item will never be null
in the example you give - usually you would access the items differently though - either by index directly (if you need the index):
XmlNodeList nodes= ...
for (int itr = 0; itr < nodes.Count; itr++)
{
//do something with nodes[i]
}
or with foreach
:
XmlNodeList nodes= ..
foreach (XmlNode node in nodes)
{
//do something with node
}
Upvotes: 2