Here is the solution in VB.NET:
************************** Begin Code *******************************
Imports System.Xml
Imports System.Xml.XPath
_
Public Class TestXML
'Entry point which delegates to C-style main Private Function
'Public Overloads Shared Sub Main()
' Main(System.Environment.GetCommandLineArgs())
'End Sub
'Public Overloads Shared Sub Main(ByVal args() As String)
' Dim xmlDocument As New XPathDocument("example.xml")
' Dim deepestElement As XPathNavigator = GetDeepestNode(xmlDocument, "*")
' If Not (deepestElement Is Nothing) Then
' Console.WriteLine("Found element {0}.", deepestElement.Name)
' Console.WriteLine(("Path is " + GetXPath(deepestElement)))
' End If
' Dim deepestNode As XPathNavigator = GetDeepestNode(xmlDocument)
' If Not (deepestElement Is Nothing) Then
' Console.WriteLine("Found node of type {0}, value {1}.", deepestNode.NodeType, deepestNode.Value)
' Console.WriteLine(("Path is " + GetXPath(deepestNode)))
' End If
'End Sub 'Main
Public Sub New()
End Sub
Public Sub GetDeepestXPath()
Dim xmlDocument As New XPathDocument("C:\xmltest.xml")
Dim deepestElement As XPathNavigator = GetDeepestNode(xmlDocument, "*")
If Not (deepestElement Is Nothing) Then
Console.WriteLine("Found element {0}.", deepestElement.Name)
Console.WriteLine(("Path is " + GetXPath(deepestElement)))
End If
Dim deepestNode As XPathNavigator = GetDeepestNode(xmlDocument)
If Not (deepestElement Is Nothing) Then
Console.WriteLine("Found node of type {0}, value {1}.", deepestNode.NodeType, deepestNode.Value)
Console.WriteLine(("Path is " + GetXPath(deepestNode)))
End If
End Sub
Public Overloads Shared Function GetDeepestNode(ByVal xmlInput As IXPathNavigable) As XPathNavigator
Return GetDeepestNode(xmlInput, "node()")
End Function 'GetDeepestNode
Public Overloads Shared Function GetDeepestNode(ByVal xmlInput As IXPathNavigable, ByVal nodeTest As String) As XPathNavigator
Dim xPathNavigator As XPathNavigator = xmlInput.CreateNavigator()
Dim xPathExpression As XPathExpression = xPathNavigator.Compile(("//" + nodeTest))
xPathExpression.AddSort("count(ancestor::node())", XmlSortOrder.Descending, XmlCaseOrder.None, "", XmlDataType.Number)
Dim nodeIterator As XPathNodeIterator = xPathNavigator.Select(xPathExpression)
If nodeIterator.MoveNext() Then
Return nodeIterator.Current
Else
Return Nothing
End If
End Function 'GetDeepestNode
Public Overloads Shared Function GetXPath(ByVal navigator As XPathNavigator) As String
Return GetXPath(navigator, "")
End Function 'GetXPath
Public Overloads Shared Function GetXPath(ByVal navigator As XPathNavigator, ByVal currentPath As String) As String
Dim name As String = ""
Select Case navigator.NodeType
Case XPathNodeType.Root
Return "/" + currentPath
Case XPathNodeType.Element
name = navigator.Name
GoTo CaseXPathNodeTypeDotText
Case XPathNodeType.Comment
name = "comment()"
GoTo CaseXPathNodeTypeDotText
Case XPathNodeType.ProcessingInstruction
name = "processing-instruction()"
GoTo CaseXPathNodeTypeDotText
Case XPathNodeType.Text
CaseXPathNodeTypeDotText:
If name = "" Then
name = "text()"
End If
Dim position As Integer = Convert.ToInt32(CDbl(navigator.Evaluate(("count(preceding-sibling::" + name + ")")))) + 1
navigator.MoveToParent()
Dim someString As String
If currentPath <> "" Then
'someString = name & "[" & position & "]" & "/" & currentPath
someString = name & "/" & currentPath
Else
'someString = name & "[" & position & "]"
someString = name
End If
'Return GetXPath(navigator, name + "[" + position + "]" +(If currentPath <> "" Then "/" + CurrentPath Else "")) 'ToDo: Unsupported feature: conditional (?) operator.
Return GetXPath(navigator, someString)
Case Else
Return ""
End Select
End Function 'GetXPath
End Class 'Test
******************************************* End Code ****************************************
Goran Djuranovic
Hi All,
Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this:
<Root>
<Customer>
<Name>MyName</Name>
</Customer>
</Root>
I would like to retreive "\Root\Customer\Name" out of it. Something like:
Dim xmlDoc As XMLDocument
Dim strXPath As String = xmlDoc.GetXPath()
TIA
Goran Djuranovic