Solved

Check for xmlnodelist != null not enouhg?

Posted on 2012-03-23
3
1,205 Views
Last Modified: 2012-03-23
Do I also need to check for how XmlNodeList it brought back form my selectNodes() method?

I only checked the not node but my xml file has no record of <titles><yyyy>  on this iteration. why would secondaryTitles != null fail?
My code enters that if statement.
Do I need to check for .count = 0 property too?

 foreach (XmlNode element in elements)
{
 

XmlNodeList secondaryTitles = element.SelectNodes("titles/yyyy"); <===
                  if (secondaryTitles != null)                     <=== count is 0 and the code enters here
                {
                    foreach (XmlNode node in secondaryTitles)
                    {
                                doing somehting here
                     }
                 }
}
0
Comment
Question by:dkim18
  • 2
3 Comments
 

Author Comment

by:dkim18
ID: 37757007
After each loop, do I need to set those xmlNodeList to null?
0
 

Author Comment

by:dkim18
ID: 37757012
When I debug it and check the line I am no on yet, the count property of the xmlnodelist has value already.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37757047
XmlNodeList secondaryTitles = element.SelectNodes("titles/yyyy");

To my understanding that WILL always return an object of the type XmlNodeList.  In other words the object won't be null.
However it is a collection of the nodes that match - having a count of 0 means that no nodes matched.

So to check if any nodes were found you must check if the count not 0, not if the object is null.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now