循环遍历 XML 文件 VB.NET
Looping Through XML File VB.NET
我有 XML 个文件:
<?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>
我有 UserControl :
我有一个 FlowLayoutPanel,它有一个 FlowDirection (TopDown)
我需要让程序使用 XML 文件中的信息在 FlowLayoutPanel 中添加新的 UserControl
例子:
程序会在Panel中添加3个UserControl
UserControl1 = <Name id="1">
UserControl2 = <Name id="2">
UserControl3 = <Name id="3">
...等等
我该怎么做?
试试这个
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
我有 XML 个文件:
<?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>
我有 UserControl :
我有一个 FlowLayoutPanel,它有一个 FlowDirection (TopDown)
我需要让程序使用 XML 文件中的信息在 FlowLayoutPanel 中添加新的 UserControl 例子: 程序会在Panel中添加3个UserControl
UserControl1 = <Name id="1">
UserControl2 = <Name id="2">
UserControl3 = <Name id="3">
...等等
我该怎么做?
试试这个
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