xpath表达式count()失败,异常,Expression必须求值为一个节点集

时间:2011-10-18 14:07:25

标签: c# .net xml xpath

我收到异常:System.Xml.XPath.XPathException:Expression必须求值为一个节点集。

当我运行这个我期望计算节点数量的代码时,我做错了什么?

var doc = new XPathDocument("contosoBooks.xml");
var nav = doc.CreateNavigator();

var expr = nav.Compile("count(//bookstore/book)");

var iterator = nav.Select(expr);
while (iterator.MoveNext())
{
    if (iterator.Current != null && !iterator.Current.HasChildren) continue;
    if (iterator.Current != null)
    {
        var childIter = iterator.Current.SelectChildren(XPathNodeType.Element);
        while (childIter.MoveNext())
        {
            if (childIter.Current != null) Console.WriteLine(childIter.Current.Value);
        }
    }
}

我的xml文件看起来像这样

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>
    <author>
      <first-name>Benjamin</first-name>
      <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
  </book>
  <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
    <title>The Confidence Man</title>
    <author>
      <first-name>Herman</first-name>
      <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
  </book>
  <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">
    <title>The Gorgias</title>
    <author>
      <name>Plato</name>
    </author>
    <price>9.99</price>
  </book>
</bookstore>

注意:感谢effords帮助我。我提出了这个解决方案:

var doc = new XPathDocument("contosoBooks.xml");
var nav = doc.CreateNavigator();

const string query = "count(//bookstore/book)";            
var expr = nav.Compile(query);

switch (expr.ReturnType)
{        
    case XPathResultType.Number:
        Console.WriteLine(nav.Evaluate(query));
        break;

    case XPathResultType.String:
    // case XPathResultType.Navigator:
        Console.WriteLine(nav.Evaluate(query));
        break;

    case XPathResultType.Boolean:
        Console.WriteLine(nav.Evaluate(query));
        break;

    case XPathResultType.NodeSet:
        var nodes = nav.Select(expr);
        while (nodes.MoveNext())
        {                      
            if (nodes.Current != null && !nodes.Current.HasChildren) continue;
            if (nodes.Current == null) continue;
            var childIter = nodes.Current.SelectChildren(XPathNodeType.Element);
            while (childIter.MoveNext())
            {
                if (childIter.Current != null) Console.WriteLine(childIter.Current.Value);
            }
        }
        break;

    case XPathResultType.Any:
        Console.WriteLine(nav.Evaluate(query));
        break;              

    default:
        Console.WriteLine(nav.Evaluate(query));                    
        break;
}            

4 个答案:

答案 0 :(得分:4)

在这一行:

var iterator = nav.Select(expr); 

expr必须是返回节点集的XPath表达式。您的XPath表达式是一个返回标量值的表达式,即书籍节点的数量。

答案 1 :(得分:4)

使用Evaluate,而不是选择:

double count =(double)nav.Evaluate(expr);

答案 2 :(得分:1)

只需更改为:

var expr = nav.Compile("//bookstore/book");

答案 3 :(得分:1)

var expr = nav.Compile("count(//bookstore/book)");
Console.WriteLine(nav.Evaluate(expr));