Solved

Vb.net XML Read all nodes and

Posted on 2016-08-19
4
72 Views
1 Endorsement
Last Modified: 2016-08-21
I am looking for some code, that will read an XML document including all Child Nodes no matter how deep as well
as the attributes. i have some code that does that but only goes 1 child deep and i am looking for a more dynamic
code which will go as deep as required. As an output i am looking for something like. below is an XML sample

<OrigParty type="PBXPartyType" subscribergroup="Subscribers in LATA 738" billingtype="flat rate" privacy="false" cpc="normal" ani-ii="00">
				<TrunkGroup type="sip">
					<TrunkGroupId>65535</TrunkGroupId>
					<TrunkMemberId>0</TrunkMemberId>
				</TrunkGroup>
				<SubscriberAddr type="e164">+12095551122</SubscriberAddr>
				<CallingPartyAddr type="e164">+12095551122</CallingPartyAddr>
				<ChargeAddr type="e164">+12095551122</ChargeAddr>
				<SIPCallId>32822942-575B11E6-95DAB3D4-69DB7200@192.168.233.30</SIPCallId>
			</OrigParty>

Open in new window

1
Comment
Question by:AlexPonnath
  • 3
4 Comments
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 500 total points
ID: 41763605
Hi Alex;

The following code snippet should do what you need.
Dim xdoc = XDocument.Load("OrigParty.xml")

Dim results = (From n In xdoc.Descendants()
                Select New With {
                    .Nodename = n.Name.LocalName,
                    .InnerText = n.Value,
                    .Attributes = n.Attributes.Select(Function(a) New With {.AttributeName = a.Name.LocalName, .Value = a.Value}).ToList()
                }).ToList()

Open in new window

The results contain a List of Anonymous objects containing information Nodename, InnerText for the node, and a list of Anonymous attributes its name and value.
0
 

Author Comment

by:AlexPonnath
ID: 41763883
is there any way to print all parent Nodes  before the node.LocalNmae ?

i would like to have a output where names would be like

RootNode
RootNode-1StChild
RootNode-1stChild-SecondChild
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 41764026
That was not stated in the original question. Please show exactly what you want to receive back from the XML document. If a class for each element and what properties you want in the class. If just a string for each node what exactly do you want in it. Please be specific.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 41764403
The following code snippet should meet your latest request. The dataList object holds the resulting data.
'' Holds the resulting data
Dim dataList As New List(Of Data)
'' Load the XML document
Dim xdoc = XDocument.Load("OrigParty.xml")
'' Get a regerence to the root node
Dim root = xdoc.Root
'' First take care of the root node 
Dim nodeData As New Data()
nodeData.Ancestors = String.Empty
nodeData.NodeName = root.Name.LocalName
nodeData.Attributes = (From a In root.Attributes()
                        Select New Attributes With {
                            .Name = a.Name.LocalName,
                            .Value = a.Value
                        }).ToList()
dataList.Add(nodeData)
'' Now take care of the child nodes
For Each element As XElement In root.Descendants()
    Dim child As New Data()
    child.Ancestors = element.Ancestors().Select(Function(a) a.Name.LocalName).Aggregate(Function(current, accumulated) accumulated & "-" & current)
    child.NodeName = element.Name.LocalName
    child.Attributes = (From a In element.Attributes()
                        Select New Attributes With {
                            .Name = a.Name.LocalName,
                            .Value = a.Value
                        }).ToList()
    dataList.Add(child)
Next

'' Holds node data
Public Class Data
    Public Property Ancestors As String
    Public Property NodeName As String
    Public Property Attributes As New List(Of Attributes)
End Class

'' Holds Attribute data
Public Class Attributes
    Public Property Name As String
    Public Property Value As String
End Class

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

21 Experts available now in Live!

Get 1:1 Help Now