Solved

Check for xmlnodelist != null not enouhg?

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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