获取数据库绑定gridview中的xml文件的值

时间:2011-05-05 21:13:39

标签: .net xml

您好 我有一个带有一个列(课程代码)的数据库,它来自一个主xmlfile。除了课程代码,数据库还保存了一些其他数据,如开始日期,结束日期等。我需要定期更新这些数据,因此我使用绑定到数据库的gridview。问题是课程名称仅在xml文件中。有没有办法可以链接数据库和xml文件(就像在关系数据库中的表一样)来在gridview中呈现这些数据。我有一个去,但结果页面非常慢,所以我认为它不能这样做。可以这样做吗?

courses.aspx

 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ myconn %>" 
    SelectCommand="SELECT CourseCode, Data1, Data2, CourseType FROM Courses ORDER BY CourseCode"> 
     </asp:SqlDataSource>

      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="CourseCode" DataSourceID="SqlDataSource1">
    <Columns>
         <asp:TemplateField HeaderText="CourseCode" SortExpression="CourseCode">

            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Bind("CourseCode") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

            <asp:TemplateField HeaderText="Name">
         <ItemTemplate>
         <asp:Label id="lblDate" runat="server"></asp:Label>
         </ItemTemplate>
         </asp:TemplateField>

         restofgridviewhere

        </Columns>
       </asp:GridView>

courses.aspx.vb

       Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
    For i As Integer = 0 To GridView1.Rows.Count - 1
        'create new label with the contents of lblDate 
        Dim labelDate As Label = DirectCast(GridView1.Rows(i).FindControl("lblDate"), Label)
        Dim Label4 As Label = DirectCast(GridView1.Rows(i).FindControl("Label4"), Label)

       For i As Integer = 0 To GridView1.Rows.Count - 1
       Dim labelDate As Label = DirectCast(GridView1.Rows(i).FindControl("lblDate"), Label)
        Dim Label4 As Label = DirectCast(GridView1.Rows(i).FindControl("Label4"), Label)

        Dim ds As New DataSet()
        ds.ReadXml("file.xml")
        Dim xmlDoc As New XmlDataDocument(ds)

        Dim nodeList As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("courses/course[code = '" & Label4.Text & "']")

        Dim myRow As DataRow
        For Each myNode As XmlNode In nodeList
            myRow = xmlDoc.GetRowFromElement(DirectCast(myNode, XmlElement))
            If myRow IsNot Nothing Then
                labelDate.Text = myRow("name")
            End If
        Next
       Next
      End Sub

file.xml

 <courses>
   <course>
      <name>Course name</name> 
      <code>Course code</code> 
   </course>
   <courses>

2 个答案:

答案 0 :(得分:1)

一种好的方法是将您的XML解析为Dictionary<string,string>,其中codekeynamevalue。现在,您可以使用CourseCode编写一个小的GridView辅助函数来获取CourseName。

这是一个有效的例子。

标记。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
    SelectCommand="SELECT CourseCode, Data1, Data2, CourseType FROM Courses ORDER BY CourseCode">
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="CourseCode"
    DataSourceID="SqlDataSource1">
    <Columns>
        <asp:TemplateField HeaderText="Course Code" SortExpression="CourseCode">
            <ItemTemplate>
                <asp:Label ID="CourseCodeLabel" runat="server" Text='<%# Eval("CourseCode") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Course Name">
            <ItemTemplate>
                <asp:Label ID="CourseNameLabel" runat="server" Text='<%# GetCourseNameFromCode(Eval("CourseCode")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <%--restofgridviewhere--%>
    </Columns>
</asp:GridView>

守则背后

Private Shared courseMapping As New Dictionary(Of String, String)()

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    If Not IsPostBack Then
        'uri is the path to the xml
        Dim uri As String = System.IO.Path.Combine(Server.MapPath("."), "App_Data/CourseXML.xml")
        Dim courses As XElement = XElement.Load(uri)
        For Each course As XElement In courses.Elements()
            courseMapping.Add(course.Element("code").Value, course.Element("name").Value)
        Next
    End If

End Sub

Protected Function GetCourseNameFromCode(courseCode As Object) As String
    Return courseMapping(courseCode.ToString())
End Function

也可能必须导入System.Xml.Linq

希望这有帮助。

答案 1 :(得分:0)

使用两个数据表 一个用于sql源,另一个用于sql源 在连接字符串中设置多个活动结果集(MARS)属性为true。 使用相应的数据表单元格绑定网格视图列

希望这会有用