I'm a bit confused on UnderlyingObject property in XPathNavigator.
There are 2 issues: 1) in .net version 3.5 and below, UnderlyingObject returns null value by giving the valid xpath in select. 2) in .net version 4 and above, UnderlyingObject returns different instances, though the xpaths that I used are the same.
The problem is demonstrated as following code:
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();
}
}
}
Result display when it runs with .net version <= 3.5
n1 != n2
UnderlyingObject are null
Result display when it runs with .net version >= 4
n1 != n2
UnderlyingObject not the same
In MSDN description, I assume n1.UnderlyingObject == n2.UnderlyingObject as they are queried with the same xpath.
According to MSDN remarks, it says
The UnderlyingObject property should preserve object identity and should only be used to return objects that have a one-to-one correspondence to their respective overlaid items. A user should always get the same object on successive visits to the same node using the same XPathNavigator object or a cloned one.
Please help :(