0
This is the sample XML code. 

<Schedule-Tables>
  <LN-Schedule-Table>
    <Short-Name>world</Short-Name>
    <Position>Start</Position>
    <Table-entrys>
      <Application-Entry>
        <Delay>0.01</Delay>
        <Frame>gh/ho/frTrig_o</Frame>
      </Application-Entry>
      <Application-Entry>
        <Delay>0.02</Delay>
        <Frame>gh/ho/frTrig_O</Frame>
      </Application-Entry>
    </Table-entrys>
  </LN-Schedule-Table>
  <LN-Schedule-Table>
    <Short-Name>worldA</Short-Name>
    <Position>Start</Position>
    <Table-entrys>
      <Application-Entry>
        <Delay>0.03</Delay>
        <Frame>gh/ho/frTrig_oZ</Frame>
      </Application-Entry>
      <Application-Entry>
        <Delay>0.04</Delay>
        <Frame>gh/ho/frTrig_oX</Frame>
      </Application-Entry>
    </Table-entrys>
  </LN-Schedule-Table>
</Schedule-Tables>

I need to read Under "Schedule-Table" for each "LN-Schedule-Table" need "Short-Name", and under "Table-Entry" need "Delay" and "Frame" details.

I am new to xml, please help me in reading this inner details of the node. Thank you.

2 Answers 2

1

As exists many approaches, this one is one of those. But, before you start to look at code you need to pay much attention in your xml string. You are wrong in tag formation, closures, names etc. Xml need to be well formatted with the same tag name aperture and closures. No spaces in tag names. Use case sensitive approach. (You can see the difference from your code and xml string below) This code is to be considered as a base helping you understanding the mechanism. Obviously need to be completed following your necessities.

Private Sub ReadXML()

    Dim mxmlString As String = "<Schedule-Tables>
                               <LN-Schedule-Table>
                                  <Short-Name>world</Short-Name> 

                                 <Position>Start</Position>
                                 <Table-Entrys>
                                  <Application-Entry>
                                   <Delay>0.01</Delay>  
                                   <Frame>gh/ho/frTrig_o
                                                 </Frame>
                                  </Application-Entry>  

                                  <Application-Entry>
                                   <Delay>0.01</Delay>
                                   <Frame>gh/ho/frTrig_o
                                                 </Frame>
                                  </Application-Entry>

                                 </Table-Entrys>
                                </LN-Schedule-Table>

                                <LN-Schedule-Table>
                                  <Short-Name>world</Short-Name>                           
                                 <Position>Start</Position>
                                 <Table-Entrys>
                                   <Application-Entry>
                                     <Delay>0.01</Delay>  
                                     <Frame>gh/ho/frTrig_o
                                                 </Frame>
                                   </Application-Entry>  

                                   <Application-Entry>
                                     <Delay>0.01</Delay>
                                     <Frame>gh/ho/frTrig_o
                                                 </Frame>
                                   </Application-Entry>

                                 </Table-Entrys>
                               </LN-Schedule-Table>
                              </Schedule-Tables>"


    Try

        Dim mXdoc As Xml.XmlDocument = New Xml.XmlDocument

        mXdoc.LoadXml(mxmlString)

        Dim mdocElement As Xml.XmlElement = mXdoc.DocumentElement

        If mdocElement IsNot Nothing AndAlso mdocElement.HasChildNodes Then

            Dim LN_Schedule_Table As Xml.XmlNodeList = mdocElement.GetElementsByTagName("LN-Schedule-Table")
            Dim Table_Entrys As Xml.XmlNodeList = mdocElement.GetElementsByTagName("Table-Entrys")

            For Each mLN_Schedule_Table As Xml.XmlElement In LN_Schedule_Table
                Dim Short_Names As Xml.XmlNodeList = mLN_Schedule_Table.GetElementsByTagName("Short-Name")

                For Each mShort_Name As Xml.XmlNode In Short_Names

                    Console.WriteLine("mShort_Name.InnerText = " & mShort_Name.InnerText)

                Next

            Next

            For Each mTable_Entry As Xml.XmlElement In Table_Entrys
                Dim Delays As Xml.XmlNodeList = mTable_Entry.GetElementsByTagName("Delay")
                Dim Frames As Xml.XmlNodeList = mTable_Entry.GetElementsByTagName("Frame")


                For Each mDelay As Xml.XmlNode In Delays

                    Console.WriteLine("mDelay.InnerText = " & mDelay.InnerText)

                Next


                For Each mFrame As Xml.XmlNode In Frames

                    Console.WriteLine("mFrame.InnerText = " & mFrame.InnerText)

                Next

            Next

        End If


    Catch ex As Exception
        Console.WriteLine(ex.ToString)
    End Try

End Sub

Or

To have all in a shot you can use LINQ. Use this example to have a big picture on mechanism. Pay attention in hierarchy descendants and in XML formatting as I wrote in the previous example above.

  Private Sub ReadXML()


        Try

            Dim mxmlString As String = "<Schedule-Tables>
                               <LN-Schedule-Table>

                                  <Short-Name>World</Short-Name> 
                                  <Position>Start</Position>

                                 <Table-Entrys>

                                  <Application-Entry>
                                   <Delay>0.01</Delay>  
                                   <Frame>gh/ho/frTrig_o</Frame>                                                 
                                  </Application-Entry>  

                                  <Application-Entry>
                                   <Delay>0.01</Delay>
                                   <Frame>gh/ho/frTrig_o</Frame>
                                  </Application-Entry>

                                 </Table-Entrys>
                                </LN-Schedule-Table>

                                <LN-Schedule-Table>

                                 <Short-Name>Mars</Short-Name>                           
                                 <Position>End</Position>

                                 <Table-Entrys>
                                   <Application-Entry>
                                     <Delay>0.01</Delay>  
                                     <Frame>gh/ho/frTrig_o</Frame>
                                   </Application-Entry>  

                                   <Application-Entry>
                                     <Delay>0.01</Delay>
                                     <Frame>gh/ho/frTrig_o</Frame>
                                  </Application-Entry>
                                 </Table-Entrys>

                               </LN-Schedule-Table>
                              </Schedule-Tables>"


            Dim mXEL As XElement = XElement.Parse(mxmlString)
            Dim dataInAShot = From ScheduleTable In mXEL.Elements("LN-Schedule-Table").AsEnumerable
                              Select ShortName = ScheduleTable.Descendants("Short-Name").Value,
                                 Position = ScheduleTable.Descendants("Position").Value,
                                 Delay = (ScheduleTable.Descendants("Table-Entrys")).Descendants("Application-Entry").Descendants("Delay").Value,
                                 Frame = (ScheduleTable.Descendants("Table-Entrys")).Descendants("Application-Entry").Descendants("Frame").Value



            For Each mData In dataInAShot

                Console.WriteLine("ShortName = " & mData.ShortName)
                Console.WriteLine("Position = " & mData.Position)
                Console.WriteLine("Delay = " & mData.Delay)
                Console.WriteLine("Frame = " & mData.Frame)
                Console.WriteLine("============================== ")

            Next



        Catch ex As Exception
            Console.WriteLine(ex.ToString)
        End Try

    End Sub
Sign up to request clarification or add additional context in comments.

9 Comments

I need corresponding schedule table's delay and frame names in a loop. The above script is giving me all schedule table names together and all delay and frame names following. Pls provide me how to get in order. Schedulee table <short name>| Delay | Frame|.
How can i get values respective to the schedule table name in each loop, in the next loop for the next schedule table and its respective values. Please help me how to get this
I’ve added another piece of code. To have all in a shot use the second one. I hope that might help you
I used Dim mXEL As Element = XElement.Load("BoardNet.xml"). For me it is not going inside the loop. Can you please help?
In the script "Dim dataInAShot = From ScheduleTable in mXEL". In this Scheduletable which one it is referring?.
|
0

I used XElement. Let me suggest that you put this in a Button Click and step through it. I had to fix the XML to get this to run.

    Dim xe As XElement
    ' to load from a URI
    '   xe = XElement.Load("path here")
    ' for testing use a literal
    xe = <Schedule-Tables>
             <LN-Schedule-Table>
                 <Short-Name>world</Short-Name>
                 <Position>Start</Position>
                 <Table-entrys>
                     <Application-Entry>
                         <Delay>0.01</Delay>
                         <Frame>gh/ho/frTrig_o</Frame>
                     </Application-Entry>
                     <Application-Entry>
                         <Delay>0.02</Delay>
                         <Frame>gh/ho/frTrig_O</Frame>
                     </Application-Entry>
                 </Table-entrys>
             </LN-Schedule-Table>
             <LN-Schedule-Table>
                 <Short-Name>worldA</Short-Name>
                 <Position>Start</Position>
                 <Table-entrys>
                     <Application-Entry>
                         <Delay>0.03</Delay>
                         <Frame>gh/ho/frTrig_oZ</Frame>
                     </Application-Entry>
                     <Application-Entry>
                         <Delay>0.04</Delay>
                         <Frame>gh/ho/frTrig_oX</Frame>
                     </Application-Entry>
                 </Table-entrys>
             </LN-Schedule-Table>
         </Schedule-Tables>

    For Each el As XElement In xe.<LN-Schedule-Table>
        Debug.WriteLine(el.<Short-Name>.Value)
        For Each sel As XElement In el...<Application-Entry>
            Debug.WriteLine(sel.<Delay>.Value)
            Debug.WriteLine(sel.<Frame>.Value)
        Next
    Next

3 Comments

It is not going inside the first for loop getting ended.
@RakeshRadhakrishnan - I don't understand your comment. The code I posted works.
It is not going inside the for loop itself if I use XElement. If I use XmlNode it is showing values, but how to take corresponding values in each loop according to the <schedule> tag name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.