Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

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

0
Charles Baldo
Asked:
Charles Baldo
1 Solution
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now