Xpath c#

  • Thread starter Thread starter csharpula csharp
  • Start date Start date
C

csharpula csharp

Hello I tried to validate XPathExpression against some xml file.
I did it this way:

XPathDocument doc = new XPathDocument(@"C:\aaa.htm");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression Expr = nav.Compile("HTML/BODY/TABLE/aaaaa");

The problem is that XPathResultType of Expr is NodeSet. How can it be
if there is no such node and how can I verify if there is or there is no
such node in xml?

Thanks!
 
csharpula said:
Hello I tried to validate XPathExpression against some xml file.
I did it this way:

XPathDocument doc = new XPathDocument(@"C:\aaa.htm");
XPathNavigator nav = doc.CreateNavigator();
XPathExpression Expr = nav.Compile("HTML/BODY/TABLE/aaaaa");

The problem is that XPathResultType of Expr is NodeSet. How can it be
if there is no such node and how can I verify if there is or there is no
such node in xml?

Well a set can be empty so I am not sure why you don't like the result
tyype.
If all you want to check if an expression selects at least one node then
do e.g.
if (nav.SelectSingleNode(Expr) != null)
{
// found one node
}
else
{
// no node found
}

Or if you are interested in all nodes then do e.g.
XPathNodeIterator nodes = nav.Select(Expr);
then you can iterate over nodes and you can also check its Count property.
 
Back
Top