473,387 Members | 1,440 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Feb 28 '06 #1
3 1448
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(pr eceding-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
"Goran Djuranovic" <djurag@mmcREMOVE_TO_MAIL.org> wrote in message news:%2***************@TK2MSFTNGP09.phx.gbl...
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
Mar 1 '06 #2
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" wrote:
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(pr eceding-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
"Goran Djuranovic" <djurag@mmcREMOVE_TO_MAIL.org> wrote in message news:%2***************@TK2MSFTNGP09.phx.gbl...
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

Mar 1 '06 #3
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(pr eceding-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

"Goran Djuranovic" <djurag@mmcREMOVE_TO_MAIL.org> wrote in message news:%2***************@TK2MSFTNGP09.phx.gbl...
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
Mar 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
7
by: Sebastian Petzelberger | last post by:
Hi group, please give me an example of a xpath with regex or better a link with examples. Thanks in advance, Sebastian
4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
5
by: Jeroen Ceuppens | last post by:
I need to put a new node at the end of the tree, that end is not te lowest in de list but the deepest (the one with the most + before it) Node A Node 1 Node 2 Node 3 Node 4: Deepest Node B:...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
3
by: Goran Djuranovic | last post by:
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>...
0
by: Goran Djuranovic | last post by:
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>...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.