XML Node Data Parsing

itman0806

New member
Joined
May 20, 2025
Messages
2
Programming Experience
3-5
Thank you for taking a look at my post.

I am working with an XML file that repeats nodes. I can get information from the nodes however I can't seem to keep it together due to a secondary repeating node. I have been able to use xdoc.Decendants("GROUP") and loop through each element to get the first 5 nodes. However I am running into trouble retrieving the areas/area tags. I tested nesting a xdoc.decendants (Not the best option :)) but this returns all the data with that tag. I also looked at XMLDocument Select Nodes, but that has the same result.

How can I get just the data between the "group" tags? Could I get a nudge in the right direction.

Thank you for your time.

Paul

tested this too:
            Dim xdoc As XDocument = XDocument.Load(strFile)
            Dim AlertsRec = xdoc.Descendants("GROUP")

'Then loop through each element in a for each statement.

            Dim xDom As New XmlDocument

            xDom.Load(strData)

            Dim MyNodeList As XmlNodeList
            Dim LocationsNodeList As XmlNodeList
            Dim LocationNodeData As XmlNodeList

            MyNodeList = xDom.SelectNodes("/GROUPS/GROUP")

Dim AlertsRec = xdoc.Descendants("ALERT")

'Then loop through each element in a for each statement.

XML structure:
<GROUPS>
    <GROUP>
        <CAPTION>Data</CAPTION>
        <CLEAN-CAPTION>Clean Data</CLEAN-CAPTION>
        <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
        <CRAWL>Text</CRAWL>
        <MESSAGE>More detailed text</MESSAGE>
        <AREAS>
            <AREA>
                <NAME>a place</NAME>
                <PLACE>Location</PLACE>
                <STREET>Street Name</STREET>
                <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
            </AREA>
            <AREA>
                <NAME>a place</NAME>
                <PLACE>Location</PLACE>
                <STREET>Street Name</STREET>
                <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
            </AREA>
            <AREA>
                <NAME>a place</NAME>
                <PLACE>Location</PLACE>
                <STREET>Street Name</STREET>
                <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
            </AREA>
        </AREAS>
    </GROUP>
    <GROUP>
        <CAPTION>Data</CAPTION>
        <CLEAN-CAPTION>Clean Data</CLEAN-CAPTION>
        <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
        <CRAWL>Text</CRAWL>
        <MESSAGE>More detailed text</MESSAGE>
        <AREAS>
            <AREA>
                <NAME>a place</NAME>
                <PLACE>Location</PLACE>
                <STREET>Street Name</STREET>
                <EXPIRATION-TIME>Date in ZULU</EXPIRATION-TIME>
            </AREA>
        </AREAS>
    </GROUP>
</GROUPS>
 
Solution
You would query each result node from within the loop, and not query from root every time. This example uses xml axis properies instead of the axis methods:
VB.NET:
Dim groups = xdoc...<GROUP>
For Each group In groups
    Debug.WriteLine("Group caption: " & group.<CAPTION>.Value)

    For Each area In group...<AREA>
        Debug.WriteLine("Area name: " & area.<NAME>.Value)
    Next
Next
You would query each result node from within the loop, and not query from root every time. This example uses xml axis properies instead of the axis methods:
VB.NET:
Dim groups = xdoc...<GROUP>
For Each group In groups
    Debug.WriteLine("Group caption: " & group.<CAPTION>.Value)

    For Each area In group...<AREA>
        Debug.WriteLine("Area name: " & area.<NAME>.Value)
    Next
Next
 
Solution
Back
Top