Solved

Vb.net XML Read all nodes and

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

622 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