Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find a path of each XML element

Posted on 2014-03-24
4
Medium Priority
?
490 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 64

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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