Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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..

XML
<PointRecord ID="00000059" TimeStamp="2019-03-16T11:53:43">
            <Name>1A</Name>
            <Method>DirectReading</Method>
            <Features>
                <Feature Name="elCABL">
                    <Attribute>
                        <Name>DSS Code</Name>
                        <Value>21</Value>
                    </Attribute>
                    <Attribute>
                        <Name>Depth (m)</Name>
                        <Value>0.8</Value>
                    </Attribute>
                    <Attribute>
                        <Name>Class</Name>
                        <Value>B</Value>
                    </Attribute>
                </Feature>
            </Features>
</PointRecord>

<PointRecord ID="00000060" TimeStamp="2019-03-16T11:54:33">
            <Name>1B</Name>
            <Method>DirectReading</Method>
</PointRecord>

<PointRecord ID="00000061" TimeStamp="2019-03-16T11:55:33">
            <Name>1C</Name>
            <Method>DirectReading</Method>
            <Features>
                <Feature Name="elPole">
                    <Attribute>
                        <Name>DSS Code</Name>
                        <Value>23</Value>
                    </Attribute>
                </Feature>
            </Features>
</PointRecord>


What I have tried:

Dim aux As New XmlDocument()
Dim PointLst As XmlNodeList        
Dim Name as String = String.Empty
aux.Load("test.xml")

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
    Next

Posted
Updated 24-Mar-19 1:29am

1 solution

I prefer to use XDocument class[^].


VB.NET
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 _
		}) _
	) _
	.ToList()


Result:
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