从xml填充DropDownList行源

时间:2013-10-13 14:12:46

标签: c# asp.net xml

我一直在尝试将xml文件中的属性绑定到包含唯一值的下拉列表。该属性是'season'标签中的'yearno'。示例xml文件仅包含一个季节的单个值,但确实会出现多个值。但是,行源数据应仅包含每个季节一次。 到目前为止,我没有运气让季节显示在下拉列表中。我在XmlDataSource的XPath中尝试了几个不同的选项,但没有用。

有人可以帮帮我吗?

以下是页面代码的一部分:

 <%@ Page Title="Activiteiten" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Activities_past_CPY.aspx.cs" Inherits="LoadLectureList.Activities" />

 <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">


  <p>
    <asp:DropDownList ID="Seasonlist" runat="server" Width="130" DataSourceID="XmlDataSource2" AppendDataBoundItems="true" />
  </p>

    <p>
    <asp:XmlDataSource ID="XmlDataSource2" runat="server" 
        DataFile="~/Lectures.xml" 
        XPath="//lectures/season[@yearno]/lecture" />

  </p>

  </asp:Content>

这是XML文件:

  

`<?xml version="1.0" encoding="UTF-8"?>
<lectures>
  <season yearno="2012">
    <lecture>
      <date>26 september 2011</date>
      <speaker>Dhr. Sylvain Ephimenco</speaker>
      <speakerdsc>
          Experience of the speaker.
      </speakerdsc>
      <subject>De Arabische volksopstanden</subject>
      <subjectdsc>
          Interesting subject
      </subjectdsc>
      <speakerpic>sylvain-ephimenco.jpg</speakerpic>
      <subjectpic1></subjectpic1>
      <subjectpic2></subjectpic2>
    </lecture>
  </season>
  <season yearno="2013">
    <lecture>
      <date>14 januari 2013</date>
      <speaker>Mr. Wim A. Egger</speaker>
      <speakerdsc>
        More experience of this speaker.
      </speakerdsc>
      <subject>Surinaamse rug, Joodse buik</subject>
      <subjectdsc>
        Another interesting subject
      </subjectdsc>
      <speakerpic>wim egger.jpg</speakerpic>
      <subjectpic1>rug_buik.jpg</subjectpic1>
      <subjectpic2></subjectpic2>
    </lecture>
  </season>
  <season yearno="2014">
    <lecture>
      <date>10 maart 2014</date>
      <speaker>Drs. C. Weltevrede</speaker>
      <speakerdsc>
        This man's curriculum.
      </speakerdsc>
      <subject>De joodse gemeente van Dordrecht tussen 1933 en heden</subject>
      <subjectdsc>
        Gotta hear this lecture
      </subjectdsc>
      <speakerpic>cees_weltevrede.jpg</speakerpic>
      <subjectpic1>synagoge_dordrecht.jpg</subjectpic1>
      <subjectpic2>tallit.tif</subjectpic2>
    </lecture>
  </season>
  <season yearno="2014">
    <lecture>
          <datum>28 april 2014</datum>
      <speaker>Prof. dr. P. W. van der Horst </speaker>
      <speakerdsc>
        Life course of this speaker.
      </speakerdsc>
      <subject>Joodse cultuur tussen Bijbel en Misjna</subject>
      <subjectdsc>
        Don't miss this one
      </subjectdsc>
      <speakerpic></speakerpic>
      <subjectpic1>talmoed.jpg</subjectpic1>
      <subjectpic2></subjectpic2>
    </lecture>
  </season>
</lectures>
`

我搜索过并在论坛上找到了很多方法,但却没有完成我的目标。任何帮助将非常感激。提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为最好的方法是按数据集读取xml文件。之后,您可以将数据表绑定到下拉列表,而不会出现任何问题。