Solved

Find a path of each XML element

Posted on 2014-03-24
4
470 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
  • 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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