Solved

Check for xmlnodelist != null not enouhg?

Posted on 2012-03-23
3
1,218 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

20 Experts available now in Live!

Get 1:1 Help Now