none
Find a parent node in an xml file

    Question

  • I have some information in an xml file which I am using to help files types.  I am essentially reading the header rows of the files and then comparing them to the information in the xml file to see which one I have.  The xml file looks like this:

    <FILES>    
        <file type="file1">
                <header_info>
                    <ID_row_1>
                        <col1>type 1 R1C1 ID</col1>
                        <col1_alt>type 1 R1C1 IALT</col1_alt>
                        <col2>type 1 R1C2</col2>
                        <col3>type 1 R1C3</col3>
                        <col4>type 1 R1C4</col4>
                    </ID_row_1>
                    <ID_row_2>
                        <col1>type1 R2C1</col1>
                        <col1_alt>type1 R2C2</col1_alt>
                        <col2>TYPE1 R2C3</col2>
                    </ID_row_2>
                </header_info>
                <file_info>
                    <data_type>BLAH</data_type>
                <file_info>
        </file>

        <file type="file2">
                <header_info>
                    <ID_row_1>
                        <col1>type 2 R1C1 ID</col1>
                        <col1_alt>type 2 R1C1 IALT</col1_alt>
                        <col2>type 2 R1C2</col2>
                        <col3>type 2 R1C3</col3>
                        <col4>type 2 R1C4</col4>
                    </ID_row_1>
                    <ID_row_2>
                        <col1>type2 R2C1</col1>
                        <col1_alt>type2 R2C2</col1_alt>
                        <col2>TYPE 2 R2C3</col2>
                    </ID_row_2>
                </header_info>
                <file_info>
                    <data_type>BLAH</data_type>
                <file_info>
        </file>
    </FILES>

    I have some code which reads the header info for each file.  In the event that the file has successfully been identified, I then need to find the parent attributes of the file element.  My code is currently:

        Sub another()
            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
            Dim m_node As XmlNode
            Dim i As Integer
            Dim to_check As String
            Dim all_good As Boolean
    
            'Create the XML Document
            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load("C:\TEST2.xml")
    
            m_nodelist = m_xmld.SelectNodes("FILES/file/header_info/ID_row_1")
            'should give a list of all in row 1
            For Each m_node In m_nodelist
                If m_node.HasChildNodes Then
                    For i = 0 To m_node.ChildNodes.Count - 1
                        to_check = (m_node.ChildNodes(i).InnerText)
                        check_this_one(to_check, all_good)
                    Next i
                End If
            Next
    
            If all_good = True Then
                'Need to identify the parent file attributes and display it!
            End If
    
        End Sub
    Any ideas on how to identify this??
    Saturday, April 22, 2017 3:14 PM

Answers

  • Thanks for all your help.  I took an amalgamation of all of them and when I have a match of file headers, just find the current file type using:

    MsgBox(m_node.ParentNode.ParentNode.Attributes.GetNamedItem("type").Value)

    • Marked as answer by Mister S Saturday, April 22, 2017 5:34 PM
    Saturday, April 22, 2017 5:34 PM

All replies

  • S,

    You can probably use XPath to get what you want:

    https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx

    I wouldn't do it that way though. I'd use the data to populate instances of one or more classes that it came from and then use those.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    • Edited by Frank L. Smith Saturday, April 22, 2017 3:24 PM ...forgot to add the link
    Saturday, April 22, 2017 3:23 PM
  • Mister

    not sure what your question actually is but here is an example where an element is found and then the parents of that are located.  Note that I changed one of the col1 values.

            'Dim yourpath As String = "your path here
            Dim xe As XElement
            ' to load from a file
            'xe = XElement.Load(yourpath)
            ' for testing
            xe = <FILES>
                     <file type="file1">
                         <header_info>
                             <ID_row_1>
                                 <col1>type 1 R1C1 ID</col1>
                                 <col1_alt>type 1 R1C1 IALT</col1_alt>
                                 <col2>type 1 R1C2</col2>
                                 <col3>type 1 R1C3</col3>
                                 <col4>type 1 R1C4</col4>
                             </ID_row_1>
                             <ID_row_2>
                                 <col1>type1 R2C1</col1>
                                 <col1_alt>type1 R2C2</col1_alt>
                                 <col2>TYPE1 R2C3</col2>
                             </ID_row_2>
                         </header_info>
                         <file_info>
                             <data_type>BLAH</data_type>
                         </file_info>
                     </file>
    
                     <file type="file2">
                         <header_info>
                             <ID_row_1>
                                 <col1>FIND</col1>
                                 <col1_alt>type 2 R1C1 IALT</col1_alt>
                                 <col2>type 2 R1C2</col2>
                                 <col3>type 2 R1C3</col3>
                                 <col4>type 2 R1C4</col4>
                             </ID_row_1>
                             <ID_row_2>
                                 <col1>type2 R2C1</col1>
                                 <col1_alt>type2 R2C2</col1_alt>
                                 <col2>TYPE 2 R2C3</col2>
                             </ID_row_2>
                         </header_info>
                         <file_info>
                             <data_type>BLAH</data_type>
                         </file_info>
                     </file>
                 </FILES>
    
            Dim ie As IEnumerable(Of XElement) = xe...<ID_row_1>.<col1>.Where(Function(el) el.Value = "FIND")
    
            If ie.Count = 1 Then
                Dim idXE As XElement = ie(0).Parent
                Dim headerXE As XElement = ie(0).Parent.Parent
                Dim fileXE As XElement = ie(0).Parent.Parent.Parent
                Stop
            End If
    
    FWIW - the XML you posted has errors in it.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    • Proposed as answer by Frank L. Smith Saturday, April 22, 2017 3:51 PM
    Saturday, April 22, 2017 3:50 PM
  • Thanks for all your help.  I took an amalgamation of all of them and when I have a match of file headers, just find the current file type using:

    MsgBox(m_node.ParentNode.ParentNode.Attributes.GetNamedItem("type").Value)

    • Marked as answer by Mister S Saturday, April 22, 2017 5:34 PM
    Saturday, April 22, 2017 5:34 PM