我想從xml文檔中添加項目並將其加載到列表視圖中。我不斷收到一行System.OutOfMemory異常的項目。添加(項目)。我做錯了什麼,我該如何正確地做?我從murach的開始visual basic.NET得到這段代碼。當我運行這一點,增加了項目之間的隨機空間在ListView從xml中獲取項目並將其添加到列表視圖
Structure ProjectInfo
Dim name As String
Dim fileextentions As ArrayList
Dim imagepath As String
End Structure
Dim project As ProjectInfo
Dim projects As New ArrayList()
'The project is loading
Private Sub diaNewProject_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Set the default filepath
txtFilepath.Text = "C:\Users\" & GetUserName() & "\Documents\Homerun IDE\Projects"
'Load the project types
If LoadProjects() Then
For Each Me.project In projects
'Add the items
ltvItems.Items.Add(project.name)
Next
Else
'Close the form
Me.Close()
End If
End Sub
Private Function LoadProjects() As Boolean
Dim ProjectReader As New XmlTextReader(_globals.ProjectsListFilename)
ProjectReader.WhitespaceHandling = WhitespaceHandling.None
Try
Do Until ProjectReader.Name = "projecttype"
ProjectReader.Read()
Loop
Do While ProjectReader.Read()
If ProjectReader.Name = "projecttype" Then
project.name = ProjectReader.Item(Name)
projects.Add(project)
End If
Loop
ProjectReader.Close()
Catch ex As XmlException
_logger.LogException(ex, TraceEventType.Critical, "Load Projects Error", "Make sure that your projectstypes.xml file is correctly formatted. In your settings " & _
"you can reset the file to your its default.")
Return False
End Try
Return True
End Function
我無法知道xml文件中有多少項目,所以我沒有看到一種方法來推進循環,我將如何做到這一點沒有循環? – muckdog12 2010-07-22 02:24:14
我不知道XML文件的細節,但是您可能想在此循環的底部添加「ProjectReader.Read()」。 – EndangeredMassa 2010-07-22 13:27:54