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 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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
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...
|
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:...
|
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...
|
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>...
|
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>...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |