c# - XmlDocument 中的字符串大于、小于和等于比较

标签 c# .net xpath xmldocument

我正在尝试在 XmlDocument 中进行字符串比较,以下是我的尝试。我想知道为什么前 2 个产生正确的结果,而后 2 个不返回任何结果。

我试图做的是根据日期时间字符串过滤掉节点。就像我的最后一个例子。

谢谢,

XmlNodeList test = x2PathDoc.SelectNodes("//config
                                            /pendingversion
                                              [@versionconfigid > 1002002]");

XmlNodeList test2 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@versionconfigid >'1002002']");

XmlNodeList test3 = x2PathDoc.SelectNodes("//config
                                             /pendingversion[@test > 'b']");

XmlNodeList test4 = x2PathDoc.SelectNodes("//config
                                             /pendingversion
                                               [@deploydatetime > 
                                                '2010-12-19T03:25:00-08:00']");

最佳答案

在 XPath 1.0 中,除相等比较之外的比较运算符仅适用于数字。这是因为在 XML 中您处理的是 UNICODE。因此,为了使字符串成为完整的有序数据类型,您需要 XPath 2.0 中添加的排序规则的概念。

第一个表达式显然是正确的。为什么第二个有效?因为“大于”运算符将两个参数都转换为 number()功能。

来自 http://www.w3.org/TR/xpath/#booleans

First, comparisons that involve node-sets are defined in terms of comparisons that do not involve node-sets; this is defined uniformly for =, !=, <=, <, >= and >.

并且在描述了节点集的存在性比较之后(仅当比较为真的节点集中有一个节点时比较为真):

When neither object to be compared is a node-set and the operator is <=, <, >= or >, then the objects are compared by converting both objects to numbers and comparing the numbers according to IEEE 754

关于c# - XmlDocument 中的字符串大于、小于和等于比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4466494/

相关文章:

c# - 需要专家对 .net 应用程序最快通信的评论

c# - ViewModel 在发布时丢失数据

c# - 如何使用 Lazy<StackFrame> 创建可预测的输出

c# - 将 WPF ListBox.ItemTemplate 的内容拉伸(stretch)到 ListBoxItem 宽度的最佳方法

c# - 命名空间 'Data' 中不存在类型或命名空间名称 'System' - 创建 AWS Lambda 函数

javascript - Android 网络浏览器中的 XPathEvaluator 在哪里?

python - 如何匹配 XPath (lxml) 中元素的内容?

xml - 使用xpath的数组位置

c# - 在 C# winform 中将选定的项目从一个列表框移动到另一个列表框

c# - Directory.CreateDirectory 找不到路径 c :\的一部分