Solved

Find a path of each XML element

Posted on 2014-03-24
4
466 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 62

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use odbc in vb to connect to ms sql 14 38
Tool Box 2 35
C# Offline Apllication 5 55
Get the selected ValueMember of Combobox 5 21
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now