Need XPathNavigator to tell me there is no Child Element

Hi I am new with xPathNaviagor and reading XML

I have set up an XPathDocument and an XPathNavigator then when I Iterate through some of the elements  all the child elements do not exist for each parent. For instance
in  <Test TestName="Test1"> there is no <Sharee>PeterBrady</Sharee>  so when I  do ni = nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/Sharee"); instead of telling me there is no data it returns the entire xml file.  Do I need to force a consisten child note structure for <Tests><Test> or can I detect a missing child node.

However it exists in     <Test TestName="Test3">

Any help or comments are apreciated

string fileName;
            fileName = @"c:\TestConfig.xml";
            XPathDocument doc = new XPathDocument(fileName);
            XPathNavigator nav = doc.CreateNavigator();
 
            XPathNodeIterator ni = nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/UserName");
            ni.MoveNext();
            Console.WriteLine(ni.Current.Value);
 
            ni = nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/ImportFolder");           
            ni.MoveNext();
            Console.WriteLine(ni.Current.Value);
 
            ni = nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/Sharee");
            ni.MoveNext();
            Console.WriteLine(ni.Current.Value)
 
 
            Console.ReadKey();
 
 
 
 
<?xml version="1.0" encoding="utf-8"?>
<TestConfig>
  <Tests>
 
    <Test TestName="Test1">
      <UserName>GregBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\wORK\GregBrady</UserWatchFolder>
      <TamFolder>C:\temp\wORK\wORK_Data</TamFolder>
    </Test>
 
    <Test TestName="Test2">
      <UserName>GregBrady</UserName>
      <TamFolder>C:\temp\wORK\wORK_Data</TamFolder>
    </Test>
 
    <Test TestName="Test3">
      <UserName>MarciaBrady</UserName>
      <ImportFolder>C:\ImportImages2</ImportFolder>
      <UserWatchFolder>C:\temp\wORK\MarciaBrady</UserWatchFolder>
      <TamFolder>C:\temp\wORK\wORK_Data</TamFolder>
      <Sharee>PeterBrady</Sharee>
      <ShareName>Test3Share</ShareName>
      <ShareText>Some Share Text</ShareText>
    </Test>
 
    <Test TestName="Test4">
      <UserName>GregBrady</UserName>
      <ImportFolder>C:\ImportImages</ImportFolder>
      <UserWatchFolder>C:\temp\wORK\GregBrady</UserWatchFolder>
      <TamFolder>C:\temp\wORK\wORK_Data</TamFolder>
    </Test>
 
  </Tests>
</TestConfig>

Open in new window

Charles BaldoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gcmachelCommented:
Try,

If this works...

if (nav.Count("/TestConfig/Tests/Test[@TestName='Test1']/Sharee") > 0) {
      ni = nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/Sharee");
      ni.MoveNext();
      Console.WriteLine(ni.Current.Value)
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks a ton slight modifications count is a property of select
if (nav.Select("/TestConfig/Tests/Test[@TestName='Test1']/Sharee").Count>0 ) worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.