JanjaNovak
asked on
Find a path of each XML element
I'm looping through XML elements like this:
I would like to find a way that for each found element the program prints all its parents. For the XML file below I expect the program to return the following result:
-------------XML file-------------
<main>
<first>text1</first>
<second>text2</second>
<abc>
<title>text3</title>
</abc>
</main>
-------------------------- ----------
-----------Expected result-----------
path of the element main is:
path of the element first is: /main/first
path of the element second is: /main/second
path of the element title is: /main/abc/title
-------------------------- ---------- ------
The question is how to define the value of variable ‘s’ in algorithm to achieve result expected?
Dim s as String
Dim xml As New Xml.XmlTextReader(xLocationAndXMLfileName)
While xml.Read
If xml.NodeType = XmlNodeType.Element Then
MsgBox("path of the element "+xml.Name+" is: "+s)
End If
End While
I would like to find a way that for each found element the program prints all its parents. For the XML file below I expect the program to return the following result:
-------------XML file-------------
<main>
<first>text1</first>
<second>text2</second>
<abc>
<title>text3</title>
</abc>
</main>
--------------------------
-----------Expected result-----------
path of the element main is:
path of the element first is: /main/first
path of the element second is: /main/second
path of the element title is: /main/abc/title
--------------------------
The question is how to define the value of variable ‘s’ in algorithm to achieve result expected?
Hi JanjaNovak;
A small correction to the post by jayakrishnabh. You will need to add the ToString() method to the two places where you see node.Name otherwise you will see an exception like this, "Operator '+' is not defined for types 'String' and 'System.Xml.Linq.XName'." the code below has the corrections.
A small correction to the post by jayakrishnabh. You will need to add the ToString() method to the two places where you see node.Name otherwise you will see an exception like this, "Operator '+' is not defined for types 'String' and 'System.Xml.Linq.XName'." the code below has the corrections.
Dim doc As XDocument = XDocument.Load("C:\Users\jharikrishna\Desktop\Noname2.xml")
Dim nde As String = "\" + doc.Root.Name.LocalName
For Each node As XElement In doc.Root.Descendants()
If node.HasElements Then
nde += "\" + node.Name.ToString()
Else
Console.WriteLine((nde & "\") + node.Name.ToString())
nde = "\" + doc.Root.Name.LocalName
End If
Next
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Since I haven't received a better solution I had to write function by my own.
Dim nde As String = "\" + doc.Root.Name.LocalName
For Each node As XElement In doc.Root.Descendants()
If node.HasElements Then
nde += "\" + node.Name
Else
Console.WriteLine((nde & "\") + node.Name)
nde = "\" + doc.Root.Name.LocalName
End If
Next