Solved

Vb.net XML Read all nodes and

Posted on 2016-08-19
4
143 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
[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
  • 3
4 Comments
 
LVL 63

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 63

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 63

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net Data Class 1 48
"lblTime is not declared" 3 65
VB6 calling COM DLL written in Visual Studio 2003 6 57
Need to Interpret Json in Vb.net ( .net 3.5  framework) 11 51
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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, …
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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