I am trying to figure out if I have an XmlNodeList object, and Count is开发者_StackOverflow社区 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,
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
}
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
}
to answer the (contrafactual?) "if so" question:
if (XmlNodeList.Item(0) != null && XmlNodeList.Item(0).HasChildNodes)
精彩评论