C# XPathNavigator和UnderlinegObject返回具有相同xpath的不同对象
我对XPathNavigator中的UnderlyingObject属性有点困惑 有两个问题: 1) 在.net版本3.5及更低版本中,UnderlyingObject通过在select中提供有效的xpath返回空值。 2) 在.net版本4和更高版本中,UnderlineingObject返回不同的实例,尽管我使用的XPath是相同的 该问题显示为以下代码:C# XPathNavigator和UnderlinegObject返回具有相同xpath的不同对象,c#,.net,xml,xpathnavigator,C#,.net,Xml,Xpathnavigator,我对XPathNavigator中的UnderlyingObject属性有点困惑 有两个问题: 1) 在.net版本3.5及更低版本中,UnderlyingObject通过在select中提供有效的xpath返回空值。 2) 在.net版本4和更高版本中,UnderlineingObject返回不同的实例,尽管我使用的XPath是相同的 该问题显示为以下代码: using System; using System.Collections.Generic; using Sy
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<item><name>wrench</name></item>");
XPathDocument xdoc = new XPathDocument(new XmlNodeReader(doc));
var nav = xdoc.CreateNavigator();
var n1 = nav.SelectSingleNode("/item");
var n2 = nav.SelectSingleNode("/item");
if(n1 != n2)
{
Console.WriteLine("n1 != n2");
}
if(n1.UnderlyingObject==null && n2.UnderlyingObject == null)
{
//it steps into here in .net <= 3.5
Console.WriteLine("UnderlyingObject are null");
}
else if(n1.UnderlyingObject != n2.UnderlyingObject)
{
//it steps into here in .net >= 4.0
Console.WriteLine("UnderlyingObject not the same");
}
Console.ReadLine();
}
}
}
当运行.net版本>=4时显示结果
n1 != n2
UnderlyingObject not the same
在MSDN描述中,我假设n1.underyingobject==n2.underyingobject,因为它们是使用相同的xpath查询的
根据评论,它说
UnderlineObject属性应保留对象标识,并且仅用于返回与其各自的覆盖项具有一对一对应关系的对象。用户在使用相同的XPathNavigator对象或克隆对象连续访问同一节点时,应始终获得相同的对象
请帮助:(有趣的是,但是,“应该”并不意味着“必须”。如果XPathNavigator
直接在XmlDocument
上创建,那么对象是相等的。
n1 != n2
UnderlyingObject not the same