Solved

Find a path of each XML element

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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