2011-06-27 47 views
2

我傳遞在我的C#代碼(.NET 3.5)以下內容:爲什麼.NET的XPath不喜歡[位置()樂10001]

XPathNavigator. 
    Compile(/windward-studios/Products/Product/@ProductID[position() le 10001]) 

和我得到:

System.Xml.XPath.XPathException 發生了
消息='/ windward-studios/Products/Product/@ ProductID [position() le 10001]'具有無效標記。
源=的System.Xml堆棧跟蹤: 在MS.Internal.Xml.XPath.XPathParser.CheckToken(LexKind 噸) 在MS.Internal.Xml.XPath.XPathParser.ParsePredicate在MS.Internal(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath .Xml.XPath.XPathParser.ParseStep(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) (AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseRelativeLocationPath(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelat在MS.Internal.Xml.XPath.XPathParser.ParseLocationPath(AstNode qyInput) iveLocationPath(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParsePathExpr在MS.Internal.Xml(AstNode qyInput) 。在MS.Internal.Xml.XPath.XPathParser.ParseUnaryExpr(AstNode qyInput) XPath.XPathParser.ParseUnionExpr(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseMultiplicativeExpr在MS(AstNode qyInput) 。 Internal.Xml.XPath.XPathParser.ParseAdditiveExpr(AstNode qyInput) at MS.Internal.Xml.XPath.XPathParser.ParseRelationalExpr(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseEqualityExpr(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseAndExpr(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser .ParseOrExpr(AstNode qyInput) 在MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(字符串 xpathExpresion) 在MS.Internal.Xml.XPath.QueryBuilder.Build(字符串 查詢,布爾allowVar,布爾 allowKey) at MS.Internal.Xml.XPath.QueryBuilder.Build(String query,Boolean & needContext) at System.Xml.XPat h.XPathExpression.Compile(字符串 的xpath,IXmlNamespaceResolver nsResolver) 在System.Xml.XPath.XPathNavigator.Compile(字符串 的xpath) 在WindwardReportsDrivers.net.windward.datasource.xml.XPathDataSource.XPathDocNode.SelectNodes(的XPathNavigator nav,String xpath,String sortXPath)
InnerException:

任何想法爲什麼?

謝謝 - 戴夫

回答

4

有一對夫婦與您的查詢的問題:

  1. <=,不le。如果您在XML中編寫它,請記住要跳過<
  2. 您試圖在屬性節點上應用[] - 這可能不是您想要的。也許你想... /Product[position() le 10001]/@ProductID
+0

謝謝 - 我一直忘記什麼是2.0。 (在@ProductID之後放置正確並且工作正常 - 我希望第一個10001節點和節點需要屬性。)再次,謝謝。 –

+1

@David:但是在屬性節點之後放置謂詞使得它有點多餘,因爲這使得'position()'返回屬性節點的位置(至少是希望!)是不相關的。如果您的所有產品都具有ProductID屬性,那麼該查詢應始終返回所有產品。 –

+0

要澄清任何人對此的困惑:「le」運算符僅在XPath 2.0中定義,但表達式正在傳遞給XPath 1.0處理器。 –