2009-06-12 2 views
1

에서 드롭 다운리스트로 XML 파일을로드하는 방법 파일은 다음과 같습니다는 asp.net

<?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를로드 할 필요

등 내가 asp.net하고 있지만 사용하고

나는 vb.net에 꽤 정통하다. asp.net은 완전히 새로운 것이다.

+0

드롭 다운 목록에서 원하는 텍스트/값을 원하십니까? – ScottE

+0

@ScottE 난 그냥 A71 .. 태그를 원해. –

답변

3

This article은 ASP.NET에있는 XMLDataSource를 사용하여이를 수행하는 방법을 설명합니다.

편집 : C#에서 VB 변환기 located here으로 코드를 실행 했으므로 구문이 보장되지 않습니다.

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 
+0

@ 매튜 존스 아주 좋은가이 VB 버전인가요? –

+0

"IsPostBack Then Is"는 무엇을 의미합니까? 고맙습니다. –

+0

이 경우 코드는 페이지로드시 드롭 다운 목록 만 채우고 이후 포스트 백에는 채워지지 않습니다. –

1

ASP.NET은 콘솔 앱이나 Windows 응용 프로그램에없는 도구를 제공한다. . LINQ-to-XML을 사용하여 필요한 요소를 추출하고 해당 결과를 데이터 소스로 드롭 다운에 바인딩 할 수 있습니다.

+1

@Brian Sullivan 당신이 어떻게하는지 설명해 주시겠습니까? 초보자! 덕분에 –