Looping Through XML File VB.NET

300 views Asked by At

i have XML File :

<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Disease>

<Name id="1">Info1
<SubArticle>Info1</SubArticle>
<MainArticle>Info1</MainArticle>
<Image>Info1</Image>
</Name>

<Name id="2">Info2
<SubArticle>Info2</SubArticle>
<MainArticle>Info2</MainArticle>
<Image>Info2</Image>
</Name>

<Name id="3">Info3
<SubArticle>Info3</SubArticle>
<MainArticle>Info3</MainArticle>
<Image>Info3</Image>
</Name>

</Disease>

and i have UserControl :

enter image description here

and i have a FlowLayoutPanel which have an FlowDirection (TopDown)

I need to make the program add new UserControl in the FlowLayoutPanel with the Information in the XML File Examble: The Program will add 3 UserControl in the Panel

UserControl1 =  <Name id="1">
UserControl2 =  <Name id="2"> 
UserControl3 =  <Name id="3"> 

...etc

How can i do this ?

1

There are 1 answers

5
jdweng On BEST ANSWER

Try this

Imports System.Xml
Module Module1

    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As New XmlDocument
        doc.Load(FILENAME)
        Dim names As XmlNodeList = doc.GetElementsByTagName("Name")

        Dim diseases As New List(Of Disease)
        For Each name As XmlNode In names
            Dim newDisease As New Disease
            diseases.Add(newDisease)

            newDisease.id = name.Attributes("id").Value
            newDisease.text = name.InnerText
            newDisease.subArticle = name.SelectSingleNode("SubArticle").InnerText
            newDisease.mainArticle = name.SelectSingleNode("MainArticle").InnerText
            newDisease.image = name.SelectSingleNode("Image").InnerText


        Next

    End Sub

    '  <Name id="1">
    '  Info1
    '  <SubArticle>Info1</SubArticle>
    '  <MainArticle>Info1</MainArticle>
    '  <Image>Info1</Image>
    '</Name>

End Module
Public Class Disease
    Public id As Integer
    Public text As String
    Public subArticle As String
    Public mainArticle As String
    Public image As String
End Class
​