开发者

XML: If a XmlNodeList.Count >= 1, will XmlNodeList.Item(#) ever be null?

开发者 https://www.devze.com 2023-02-22 21:11 出处:网络
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?

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消