在silverlight datagrid,vb.net中显示xml数据

时间:2010-05-12 15:50:32

标签: xml vb.net silverlight silverlight-3.0

我想在silverlight datagrid中显示xml文件数据。我使用下面的代码,但它不起作用。请帮助。

我的vb.net代码:

进口系统 Imports System.Collections.Generic 进口System.Linq 进口System.Windows 导入System.Windows.Controls 导入System.Xml.Linq

命名空间SilverlightApplication1     公共部分类         继承UserControl         Public Sub New()             的InitializeComponent()         结束子

    Private Sub Page_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        DataGrid1.ItemsSource = GetReport() 
    End Sub 

    Public Function GetStatusReport() As List(Of Table) 
        Dim statusReport As New List(Of Table)() 

        Dim doc As XElement = XElement.Load("Data/Report.xml") 

        report = (From row In doc.Elements() _ 
            Select GetStatus(row)).ToList() 

        Return statusReport 
    End Function 

    Private Function GetReport(ByVal row As XElement) As Table
        Dim s As New Table() 
        s.JobID= row.Attribute("ID").Value 
        s.VenueName= row.Attribute("Name").Value) 
        Return s 
    End Function 
End Class 

结束命名空间

1 个答案:

答案 0 :(得分:0)

我对这段代码感到很困惑......这可能就是为什么它不起作用了。

首先,当您设置ItemsSource时,不使用参数调用GetReport()。永远不会调用GetStatusReport(),即使它提升了XML。在GetStatusReport中,你返回statusReport,这是一个空列表...你永远不会对报告做任何事情,这是实际的查询。在查询中,您调用了未定义的GetStatus,但我认为它应该是GetReport。

Arghhh ......尽管如此,我猜你想重写所有代码是这样的:

    Private Sub Page_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        DataGrid1.ItemsSource = GetStatusReport() 
    End Sub 

    Public Function GetStatusReport() As List(Of Table) 
        Dim statusReport As New List(Of Table)() 

        Dim doc As XElement = XElement.Load("Data/Report.xml") 

        statusReport = (From row In doc.Elements() _ 
            Select GetReport(row)).ToList() 

        Return statusReport 
    End Function 

    Private Function GetReport(ByVal row As XElement) As Table
        Dim s As New Table() 
        s.JobID= row.Attribute("ID").Value 
        s.VenueName= row.Attribute("Name").Value) 
        Return s 
    End Function 
End Class 

除了这些修正之外,我无法告诉你它是否正确......它在哪里失败?