Solved

Vb.net XML Read all nodes and

Posted on 2016-08-19
4
62 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

16 Experts available now in Live!

Get 1:1 Help Now