How to retreive deepest XPath value from XML using VB.NET

G

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
 
G

Goran Djuranovic

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
 
G

Guest

Goran,
please don't cross post to multiple groups. Your post is clearly intended
for the vb language group; posting it to the C# group only serves to confuse
people.
Thanks,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Goran Djuranovic said:
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
 
G

Goran Djuranovic

Solution:

********************** 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top