2011-06-30 62 views
2

我創建了一個標準XsltContext類並調用它,如下所示:XPath擴展方法未能在.net 3.5,工作在.NET 4.0

 XPathCustomContext context = new XPathCustomContext(new NameTable()); 
     context.AddNamespace("windward", XPathCustomContext.Namespace); 
     XsltArgumentList varList = new XsltArgumentList(); 
     varList.AddParam("stat-index", "", 0); 
     context.ArgList = varList; 

     XmlDocument doc = new XmlDocument(); 
     doc.Load("c:\\test\\order.xml"); 
     object xx = doc.CreateNavigator().Evaluate("/order[1]/customer[1]/num[@negone = $stat-index]", context); 

當在.NET 4.0,它工作正常運行。但在.NET 3.5(我們目前必須使用)下,我得到:

System.Xml.XPath.XPathException was unhandled 
    Message=XsltContext is needed for this query because of an unknown function. 
    Source=System.Xml 
    StackTrace: 
     at MS.Internal.Xml.XPath.CompiledXpathExpr.UndefinedXsltContext.ResolveVariable(String prefix, String name) 
     at MS.Internal.Xml.XPath.VariableQuery.SetXsltContext(XsltContext context) 
     at MS.Internal.Xml.XPath.LogicalExpr.SetXsltContext(XsltContext context) 
     at MS.Internal.Xml.XPath.FilterQuery.SetXsltContext(XsltContext input) 
     at MS.Internal.Xml.XPath.CompiledXpathExpr.SetContext(XmlNamespaceManager nsManager) 
     at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver) 
     at System.Xml.XPath.XPathNavigator.Evaluate(String xpath, IXmlNamespaceResolver resolver) 
     at CustomXpathFunctions.Program.Main(String[] args) in c:\src\CustomXpathFunctions\Program.cs:line 62 
     at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 
     at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
     at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
     at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
     at System.Threading.ThreadHelper.ThreadStart() 
    InnerException: 

任何想法爲什麼?在http://www.windwardreports.com/temp/CustomXPathFunctionsBug.zip

感謝

示例代碼 - 戴夫

+0

而order.xml位於http://www.windwardreports.com/temp/order.xml –

回答

2

想出一個辦法做到這一點。使用像一個XPathExpression:

XPathNavigator nav = doc.CreateNavigator(); 
XPathExpression exp = nav.Compile("/order[1]/customer[1]/num[@negone = $stat]"); 
exp.SetContext(ctx); 
object zzz = nav.Evaluate(exp); 

至於爲什麼 - 我的猜測是評估(字符串,上下文)使用它僅在.NET 3.5的命名空間,而XPathExpression使用它的一切。關鍵點在於這個作品。

ps - 請把注意力放在這個答案中 - 這真的很難弄清楚。