如何将xml文件加载到asp.net中的dropdownlist中 [英] how to load xml file into dropdownlist in asp.net

查看:52
本文介绍了如何将xml文件加载到asp.net中的dropdownlist中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <data>
    <a7190>
    <food>Almond</food>
    <food>American Cheese</food>
    <food>Apple</food>
    </a7190>
    <a7191>
    <food>Trout</food>
    <food>Tuna</food>
</a7191>
    <food>Turkey</food>
    <food>Wheat</food>
<a7193>
    <food>Yogurt</food>
    </a7193>
    </data>

我只需要加载a7190,a7191等

i ONLY need to load the a7190, a7191, etc

我正在使用asp.net,尽管我对vb.net十分熟悉,但是asp.net对我来说是全新的

i am using asp.net and although i am pretty well-versed with vb.net, asp.net is completely new to me

推荐答案

本文介绍了如何使用ASP.NET中提供的XMLDataSource进行此操作.

This article describes how to do this using the XMLDataSource present in ASP.NET.

我只是通过C#到VB转换器运行代码位于这里,因此不能保证语法.

I just ran the code through the C# to VB converter located here, so the syntax is not guaranteed.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
         'call to the function to populate dropdownlist from xml'
         PopulateDDLFromXMLFile()
    End If
End Sub

    'populates the dropdownlist from xml file'
    Public Sub PopulateDDLFromXMLFile()
        Dim ds As New DataSet()
        ds.ReadXml(MapPath("~/Resources/XMLFile.xml"))
        
        'get the dataview of table "Country", which is default table name'
        Dim dv As DataView = ds.Tables("Country").DefaultView
        'or we can use:'
        'DataView dv = ds.Tables[0].DefaultView;'
        
        'Now sort the DataView vy column name "Name"'
        dv.Sort = "Name"
        
        'now define datatext field and datavalue field of dropdownlist'
        ddlCountry.DataTextField = "Name"
        ddlCountry.DataValueField = "ID"
        
        'now bind the dropdownlist to the dataview'
        ddlCountry.DataSource = dv
        ddlCountry.DataBind()
    End Sub

这篇关于如何将xml文件加载到asp.net中的dropdownlist中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆