ASP .NET - XML 文件

我们可以把 XML 文件绑定到列表控件。

实例

XML RadiobuttonList

  1. <%@ Import Namespace="System.Data" %>
  2. <script runat="server">
  3. sub Page_Load
  4. if Not Page.IsPostBack then
  5. dim mycountries=New DataSet
  6. mycountries.ReadXml(MapPath("countries.xml"))
  7. rb.DataSource=mycountries
  8. rb.DataValueField="value"
  9. rb.DataTextField="text"
  10. rb.DataBind()
  11. end if
  12. end sub
  13. sub displayMessage(s as Object,e As EventArgs)
  14. lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
  15. end sub
  16. </script>
  17. <html>
  18. <body>
  19. <form runat="server">
  20. <asp:RadioButtonList id="rb" runat="server"
  21. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  22. <p><asp:label id="lbl1" runat="server" /></p>
  23. </form>
  24. </body>
  25. </html>

一个 XML 文件

这里有一个名为 "countries.xml" 的 XML 文件:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <countries>
  4.  
  5. <country>
  6. <text>China</text>
  7. <value>C</value>
  8. </country>
  9.  
  10. <country>
  11. <text>Sweden</text>
  12. <value>S</value>
  13. </country>
  14.  
  15. <country>
  16. <text>France</text>
  17. <value>F</value>
  18. </country>
  19.  
  20. <country>
  21. <text>Italy</text>
  22. <value>I</value>
  23. </country>
  24.  
  25. </countries>

把 DataSet 绑定到 List 控件

首先,导入 "System.Data" 命名空间。我们需要该命名空间与 DataSet 对象一起工作。把下面这条指令包含在 .aspx 页面的顶部:

  1. <%@ Import Namespace="System.Data" %>

接下来,为这个 XML 文件创建一个 DataSet,并在页面首先加载时把这个 XML 文件载入该 DataSet:

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New DataSet
  5. mycountries.ReadXml(MapPath("countries.xml"))
  6. end if
  7. end sub

如需把该 DataSet 绑定到 RadioButtonList 控件,首先请在 .aspx 页面中创建一个 RadioButtonList 控件(没有任何 asp:ListItem 元素):

  1. <html>
  2. <body>
  3.  
  4. <form runat="server">
  5. <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
  6. </form>
  7.  
  8. </body>
  9. </html>

然后添加构建这个 XML DataSet 的脚本:

  1. <%@ Import Namespace="System.Data" %>
  2.  
  3. <script runat="server">
  4. sub Page_Load
  5. if Not Page.IsPostBack then
  6. dim mycountries=New DataSet
  7. mycountries.ReadXml(MapPath("countries.xml"))
  8. rb.DataSource=mycountries
  9. rb.DataValueField="value"
  10. rb.DataTextField="text"
  11. rb.DataBind()
  12. end if
  13. end sub
  14. </script>
  15.  
  16. <html>
  17. <body>
  18.  
  19. <form runat="server">
  20. <asp:RadioButtonList id="rb" runat="server"
  21. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  22. </form>
  23.  
  24. </body>
  25. </html>

然后,我们添加一个子例程,该子例程会在用户点击 RadioButtonList 控件中的项目时执行。当用户点击某个单选按钮时,label 中会出现一条文本:

  1. <%@ Import Namespace="System.Data" %>
  2.  
  3. <script runat="server">
  4. sub Page_Load
  5. if Not Page.IsPostBack then
  6. dim mycountries=New DataSet
  7. mycountries.ReadXml(MapPath("countries.xml"))
  8. rb.DataSource=mycountries
  9. rb.DataValueField="value"
  10. rb.DataTextField="text"
  11. rb.DataBind()
  12. end if
  13. end sub
  14.  
  15. sub displayMessage(s as Object,e As EventArgs)
  16. lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
  17. end sub
  18. </script>
  19.  
  20. <html>
  21. <body>
  22.  
  23. <form runat="server">
  24. <asp:RadioButtonList id="rb" runat="server"
  25. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  26. <p><asp:label id="lbl1" runat="server" /></p>
  27. </form>
  28.  
  29. </body>
  30. </html>