Hi Martin,
Yes, you are right. I switched "\" & "/".
Luckily, I am not using the namespaces and the code you posted is exactly
what I needed. I am posting VB.NET version below, in case someone else needs
it. And, also, thank you very much.
*************** ******* Begin Code *************** ********
Imports System.Xml
Imports System.Xml.XPat h
_
Public Class TestXML
'Entry point which delegates to C-style main Private Function
'Public Overloads Shared Sub Main()
' Main(System.Env ironment.GetCom mandLineArgs())
'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.WriteLi ne("Found element {0}.", deepestElement. Name)
' Console.WriteLi ne(("Path is " + GetXPath(deepes tElement)))
' End If
' Dim deepestNode As XPathNavigator = GetDeepestNode( xmlDocument)
' If Not (deepestElement Is Nothing) Then
' Console.WriteLi ne("Found node of type {0}, value {1}.",
deepestNode.Nod eType, deepestNode.Val ue)
' Console.WriteLi ne(("Path is " + GetXPath(deepes tNode)))
' 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.WriteLi ne("Found element {0}.", deepestElement. Name)
Console.WriteLi ne(("Path is " + GetXPath(deepes tElement)))
End If
Dim deepestNode As XPathNavigator = GetDeepestNode( xmlDocument)
If Not (deepestElement Is Nothing) Then
Console.WriteLi ne("Found node of type {0}, value {1}.",
deepestNode.Nod eType, deepestNode.Val ue)
Console.WriteLi ne(("Path is " + GetXPath(deepes tNode)))
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.Create Navigator()
Dim xPathExpression As XPathExpression = xPathNavigator. Compile(("//" +
nodeTest))
xPathExpression .AddSort("count (ancestor::node ())", XmlSortOrder.De scending,
XmlCaseOrder.No ne, "", XmlDataType.Num ber)
Dim nodeIterator As XPathNodeIterat or =
xPathNavigator. Select(xPathExp ression)
If nodeIterator.Mo veNext() Then
Return nodeIterator.Cu rrent
Else
Return Nothing
End If
End Function 'GetDeepestNode
Public Overloads Shared Function GetXPath(ByVal navigator As XPathNavigator)
As String
Return GetXPath(naviga tor, "")
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.NodeT ype
Case XPathNodeType.R oot
Return "/" + currentPath
Case XPathNodeType.E lement
name = navigator.Name
GoTo CaseXPathNodeTy peDotText
Case XPathNodeType.C omment
name = "comment()"
GoTo CaseXPathNodeTy peDotText
Case XPathNodeType.P rocessingInstru ction
name = "processing-instruction()"
GoTo CaseXPathNodeTy peDotText
Case XPathNodeType.T ext
CaseXPathNodeTy peDotText:
If name = "" Then
name = "text()"
End If
Dim position As Integer =
Convert.ToInt32 (CDbl(navigator .Evaluate(("cou nt(preceding-sibling::" + name
+ ")")))) + 1
navigator.MoveT oParent()
Dim someString As String
If currentPath <> "" Then
'someString = name & "[" & position & "]" & "/" & currentPath
someString = name & "/" & currentPath
Else
'someString = name & "[" & position & "]"
someString = name
End If
'Return GetXPath(naviga tor, name + "[" + position + "]" +(If currentPath <>
"" Then "/" + CurrentPath Else "")) 'ToDo: Unsupported feature: conditional
(?) operator.
Return GetXPath(naviga tor, someString)
Case Else
Return ""
End Select
End Function 'GetXPath
End Class 'Test
*************** ********** End Code *************** *************** *
Thanks
Goran Djuranovic
"Martin Honnen" <ma*******@yaho o.de> wrote in message
news:uC******** ******@TK2MSFTN GP14.phx.gbl...
Goran Djuranovic wrote:
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.
Well \Root and so on is not even legal XPath syntax, I guess you want
/Root/Customer/Name
but event then in terms of the XPath data model there is a leaf text node
deeper than that element so e.g.
/Root/Customer/Name/text()
or
/Root[1]/Customer[1]/Name[1]/text()[1]
might be more precise to describe/select the deepest node.
I don't have VB.NET code for that, here is some sample C# .NET code that
should do as long as no namespaces are involved:
using System.Xml;
using System.Xml.XPat h;
public class Test {
public static void Main (string[] args) {
XPathDocument xmlDocument = new XPathDocument(" example.xml");
XPathNavigator deepestElement = GetDeepestNode( xmlDocument, "*");
if (deepestElement != null) {
Console.WriteLi ne("Found element {0}.", deepestElement. Name);
Console.WriteLi ne("Path is " + GetXPath(deepes tElement));
}
XPathNavigator deepestNode = GetDeepestNode( xmlDocument);
if (deepestElement != null) {
Console.WriteLi ne("Found node of type {0}, value {1}.",
deepestNode.Nod eType, deepestNode.Val ue);
Console.WriteLi ne("Path is " + GetXPath(deepes tNode));
}
}
public static XPathNavigator GetDeepestNode (IXPathNavigabl e xmlInput) {
return GetDeepestNode( xmlInput, "node()");
}
public static XPathNavigator GetDeepestNode (IXPathNavigabl e xmlInput,
string nodeTest) {
XPathNavigator xPathNavigator = xmlInput.Create Navigator();
XPathExpression xPathExpression = xPathNavigator. Compile("//" +
nodeTest);
xPathExpression .AddSort("count (ancestor::node ())",
XmlSortOrder.De scending, XmlCaseOrder.No ne, "", XmlDataType.Num ber);
XPathNodeIterat or nodeIterator =
xPathNavigator. Select(xPathExp ression);
if (nodeIterator.M oveNext()) {
return nodeIterator.Cu rrent;
}
else {
return null;
}
}
public static string GetXPath (XPathNavigator navigator) {
return GetXPath(naviga tor, "");
}
public static string GetXPath (XPathNavigator navigator, string
currentPath) {
string name = "";
switch (navigator.Node Type) {
case XPathNodeType.R oot:
return "/" + currentPath;
case XPathNodeType.E lement:
name = navigator.Name;
goto case XPathNodeType.T ext;
case XPathNodeType.C omment:
name = "comment()" ;
goto case XPathNodeType.T ext;
case XPathNodeType.P rocessingInstru ction:
name = "processing-instruction()";
goto case XPathNodeType.T ext;
case XPathNodeType.T ext:
if (name == "") {
name = "text()";
}
int position =
Convert.ToInt32 ((double)naviga tor.Evaluate("c ount(preceding-sibling::" +
name + ")")) + 1;
navigator.MoveT oParent();
return GetXPath(naviga tor, name + "[" + position + "]" +
(currentPath != "" ? "/" + currentPath : ""));
default:
return "";
}
}
}
If namespaces are involved it gets difficult to simply return a string,
you need to use prefixes then in the XPath expression and somehow return
how those prefixes are bound to namespace URIs.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/