1

the file looks like this:

    <?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>

i ONLY need to load the a7190, a7191, etc

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

2
  • what text/value do you want in the dropdown list? Commented Jun 12, 2009 at 15:59
  • @ScottE i just want the A71.. tags Commented Jun 12, 2009 at 16:22

2 Answers 2

3

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

EDIT: 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
Sign up to request clarification or add additional context in comments.

4 Comments

@Matthew Jones very nice is there a vb version of this?
In this case, the code will only populate the dropdownlist on page load, not on any subsequent postbacks.
No, I live in Phoenix, and I have a really common name.
it doesnt understand what dim as new "dataset" means. it doenst know what dataset is
1

I don't know that ASP.NET gives you any tools here that you wouldn't have in a console app or Windows app. You might try using LINQ-to-XML to pull out the elements you need, and bind that result as a datasource to your dropdown.

1 Comment

@Brian Sullivan can you please describe how to do this. im a beginner! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.