Solved

Check for xmlnodelist != null not enouhg?

Posted on 2012-03-23
3
1,263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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