Click here to Skip to main content
15,671,318 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am struggling to get my head around how to extract inner text values of xml nodes where the parent node does not always contain the same child node. My (trimmed down version) xml file is below. I'm trying to extract the Attribute values from each PointRecord node where they exist. The Attribute may or may not exist.

I only want to extract the PointRecord 'Name' value and all the Attribute values only if the Attributes exist. :( Any ideas? Forgive me as as I'm a bit of a noob..

<PointRecord ID="00000059" TimeStamp="2019-03-16T11:53:43">
                <Feature Name="elCABL">
                        <Name>DSS Code</Name>
                        <Name>Depth (m)</Name>

<PointRecord ID="00000060" TimeStamp="2019-03-16T11:54:33">

<PointRecord ID="00000061" TimeStamp="2019-03-16T11:55:33">
                <Feature Name="elPole">
                        <Name>DSS Code</Name>

What I have tried:

Dim aux As New XmlDocument()
Dim PointLst As XmlNodeList        
Dim Name as String = String.Empty

PointLst = aux.GetElementsByTagName("PointRecord")

    For Each Att as XmlElement in PointLst

        If Att.GetElementsByTagName("Feature") IsNot Nothing Then

            Name = Att.FirstCild.InnerText
            //lost here, not sure how to procees??
        End If

Updated 24-Mar-19 1:29am

1 solution

I prefer to use XDocument class[^].

Dim result = xdoc.Root.Descendants("Feature") _
	.SelectMany(Function(x) x.Descendants("Attribute") _
		.Select(Function(y) New With _
		{ _
			.Future = x.Attribute("Name").Value, _
			.Name = y.Element("Name").Value, _
			.Value = y.Element("Value").Value _
		}) _
	) _

Future Name      Value
elCABL DSS Code  21 
elCABL Depth (m) 0.8 
elCABL Class     B 
elPole DSS Code  23 
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900