Reputation: 1
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.
Upvotes: 0
Views: 67
Reputation: 2695
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
Upvotes: 1
Reputation: 11773
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
Upvotes: 0