Solved

Find a path of each XML element

Posted on 2014-03-24
4
481 Views
Last Modified: 2014-04-01
I'm looping through XML elements like this:

            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

Open in new window


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?
0
Comment
Question by:JanjaNovak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 39950234
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
      Else
            Console.WriteLine((nde & "\") + node.Name)
            nde = "\" + doc.Root.Name.LocalName

      End If
Next
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39950299
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.
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

Open in new window

0
 

Accepted Solution

by:
JanjaNovak earned 0 total points
ID: 39959805
I managed to find a solution myself:

Function IzpisiPolneTageXMLvString(xLokacijaInImeXML As String) As String
        Try
            Dim xml As New Xml.XmlTextReader(xLokacijaInImeXML)
            Dim s As String = Nothing
            Dim t As String = Nothing
            Dim cTrenutni, cCelotni As Integer
            Dim elem As String = ""

            While xml.Read
                Select Case xml.NodeType
                    Case XmlNodeType.Element
                        elem = "/" + xml.Name
                        t = t + elem
                        cCelotni = Len(t)
                    Case XmlNodeType.Text
                        If s <> "" Then s = s + ";"
                        s = s + t
                        cCelotni = Len(t)
                    Case XmlNodeType.EndElement 
                        elem = "/" + xml.Name
                        cTrenutni = Len(elem)
                        If (cCelotni - cTrenutni) > 0 Then
                            t = Mid(t, 1, cCelotni - cTrenutni)
                            cCelotni = Len(t)
                        Else
                            t = ""
                        End If
                End Select
            End While
            xml.Close()

            IzpisiPolneTageXMLvString = s
        Catch ex As Exception
            ZapisiVLOG(System.Reflection.MethodBase.GetCurrentMethod.ToString + ": " + ex.Message, True, MsgBoxStyle.Critical)
            Application.Exit()
        End Try
    End Function

Open in new window

0
 

Author Closing Comment

by:JanjaNovak
ID: 39968801
Since I haven't received a better solution I had to write function by my own.
0

Featured Post

Command Line Tips and Tricks

The command line is a powerful tool at the disposal of every Linux user. Although Linux distros come with beautiful user interfaces, it's worthwhile to learn the command line because it allows you to do a number of things that you otherwise cannot do from the GUI.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question