从xml获取项目并添加到listview

时间:2010-07-22 02:13:50

标签: xml vb.net

我正在尝试从xml文档中添加get项并将其加载到listview中。我继续在行projects.Add(项目)上获得System.OutOfMemory异常。我做错了什么,我该怎么做?我从 murach开始使用visual basic.NET 获得了这段代码。当我运行它时,它会在listview

中的项之间添加随机空格
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

2 个答案:

答案 0 :(得分:2)

这里有一个无限循环:

Do While ProjectReader.Name = "projecttype"
        project.name = ProjectReader.Item("name")
        'TODO: extension
        'TODO: imagepath
        projects.Add(project)
Loop

您必须在循环内推进阅读器或删除此循环。

答案 1 :(得分:0)

    Do While ProjectReader.Read() 
      If ProjectReader.Name = "name" Then
        ...
      End If 
    Loop 

您还应该注意project.name = ProjectReader.Item("name")将返回属性@name的值,因此要使其工作,元素结构必须是类似的。

<name name="project name"/>

这可能不是你想要的。如果您想要元素值,请使用project.name = ProjectReader.ReadString()