Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

Vb.net XML Read all nodes and

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
AlexPonnath
Asked:
AlexPonnath
  • 3
2 Solutions
 
Fernando SotoCommented:
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
 
AlexPonnathAuthor Commented:
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
 
Fernando SotoCommented:
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
 
Fernando SotoCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now