Solved

Check for xmlnodelist != null not enouhg?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

12 Experts available now in Live!

Get 1:1 Help Now