日期:2014-05-20  浏览次数:20901 次

Repeater嵌套读取XML
C# code

<?xml version="1.0" encoding="utf-8" ?>
<lists>
  <code>0</code>
  <desc />
  <para>
    <sc>CAN</sc>
    <ec>BKK</ec>
    <sd>2011-12-01</sd>
    <ed />
    <air />
    <ft>1</ft>
    <pt />
    <ac />
  </para>
   <flights>
     <flight>
      <depport>CAN</depport>
      <arrport>BKK</arrport>
      <airline>KA</airline>
      <adultprice>930</adultprice>
      <childprice>700</childprice>
      <tax>573</tax>
      <currency>CNY</currency>
      <open>0</open>
       <deproute>
        <route>CAN-HKG-HKG-BKK</route>
        <aircode>KA</aircode>
        <depdate>2011-12-01</depdate>
        <arrdate>2011-12-01</arrdate>
        <deptime>0950</deptime>
        <arrtime>1620</arrtime>
        <transfer>1</transfer>
        <seat>Y</seat>
      </deproute>
       <arrroute>
        <route />
        <aircode />
        <depdate />
        <arrdate />
        <deptime />
        <arrtime />
        <transfer />
        <seat />
      </arrroute>
       <flightdatas>
         <segment>
          <dep>CAN</dep>
          <arr>HKG</arr>
          <air>KA</air>
          <flightno>KA783</flightno>
          <depairport>CAN</depairport>
          <arrairport>HKG</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>0950</deptime>
          <arrtime>1055</arrtime>
          <flytime>1.5</flytime>
          <plane>330</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
        <segment>
          <dep>HKG</dep>
          <arr>BKK</arr>
          <air>CX</air>
          <flightno>CX751</flightno>
          <depairport>HKG</depairport>
          <arrairport>BKK</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>1425</deptime>
          <arrtime>1620</arrtime>
          <flytime>1.55</flytime>
          <plane>330</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
      </flightdatas>
      <rulestr>C9533CB663235419CC45504BA0E0AEE8</rulestr>
    </flight>
     <flight>
      <depport>CAN</depport>
      <arrport>BKK</arrport>
      <airline>SQ</airline>
      <adultprice>1020</adultprice>
      <childprice>820</childprice>
      <tax>1099</tax>
      <currency>CNY</currency>
      <open>0</open>
       <deproute>
        <route>CAN-SIN-SIN-BKK</route>
        <aircode>SQ</aircode>
        <depdate>2011-12-01</depdate>
        <arrdate>2011-12-01</arrdate>
        <deptime>0150</deptime>
        <arrtime>0840</arrtime>
        <transfer>1</transfer>
        <seat>Y</seat>
      </deproute>
       <arrroute>
        <route />
        <aircode />
        <depdate />
        <arrdate />
        <deptime />
        <arrtime />
        <transfer />
        <seat />
      </arrroute>
       <flightdatas>
         <segment>
          <dep>CAN</dep>
          <arr>SIN</arr>
          <air>SQ</air>
          <flightno>SQ853</flightno>
          <depairport>CAN</depairport>
          <arrairport>SIN</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>0150</deptime>
          <arrtime>0610</arrtime>
          <flytime>4.20</flytime>
          <plane>333</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
         <segment>
          <dep>SIN</dep>
          <arr>BKK</arr>
          <air>SQ</air>
          <flightno>SQ970</flightno>
          <depairport>SIN</depairport>
          <arrairport>BKK</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>0715</deptime>
          <arrtime>0840</arrtime>
          <flytime>1.25</flytime>
          <plane>772</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
      </flightdatas>
      <rulestr>F9EEB3340D3590EDBAE02D6E2CB083ED</rulestr>
    </flight>
     <flight>
      <depport>CAN</depport>
      <arrport>BKK</arrport>
      <airline>CZ</airline>
      <adultprice>1040</adultprice>
      <childprice>870</childprice>
      <tax>416</tax>
      <currency>CNY</currency>
      <open>0</open>
       <deproute>
        <route>CAN-BKK</route>
        <aircode>CZ</aircode>
        <depdate>2011-12-01</depdate>
        <arrdate>2011-12-01</arrdate>
        <deptime>0900</deptime>
        <arrtime>1050</arrtime>
        <transfer>0</transfer>
        <seat>Y-Y</seat>
      </deproute>
       <arrroute>
        <route />
        <aircode />
        <depdate />
        <arrdate />
        <deptime />
        <arrtime />
        <transfer />
        <seat />
      </arrroute>
       <flightdatas>
        <segment>
          <dep>CAN</dep>
          <arr>BKK</arr>
          <air>CZ</air>
          <flightno>CZ357</flightno>
          <depairport>CAN</depairport>
          <arrairport>BKK</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>0900</deptime>
          <arrtime>1050</arrtime>
          <flytime>1.50</flytime>
          <plane>320</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
      </flightdatas>
      <rulestr>5AC4661F9FB407FBB76B17034181C810</rulestr>
    </flight>
     <flight>
      <depport>CAN</depport>
      <arrport>BKK</arrport>
      <airline>TG</airline>
      <adultprice>1210</adultprice>
      <childprice>960</childprice>
      <tax>406</tax>
      <currency>CNY</currency>
      <open>0</open>
      <deproute>
        <route>CAN-BKK</route>
        <aircode>TG</aircode>
        <depdate>2011-12-01</depdate>
        <arrdate>2011-12-01</arrdate>
        <deptime>1440</deptime>
        <arrtime>1700</arrtime>
        <transfer>0</transfer>
        <seat>Y-Y</seat>
      </deproute>
       <arrroute>
        <route />
        <aircode />
        <depdate />
        <arrdate />
        <deptime />
        <arrtime />
        <transfer />
        <seat />
      </arrroute>
      <flightdatas>
         <segment>
          <dep>CAN</dep>
          <arr>BKK</arr>
          <air>TG</air>
          <flightno>TG669</flightno>
          <depairport>CAN</depairport>
          <arrairport>BKK</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>1440</deptime>
          <arrtime>1700</arrtime>
          <flytime>2.20</flytime>
          <plane>330</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
      </flightdatas>
      <rulestr>08EEF892D353760EF3ADCC65EEA270C8</rulestr>
    </flight>
     <flight>
      <depport>CAN</depport>
      <arrport>BKK</arrport>
      <airline>OZ</airline>
      <adultprice>2670</adultprice>
      <childprice>2130</childprice>
      <tax>0</tax>
      <currency>CNY</currency>
      <open>0</open>
       <deproute>
        <route>CAN-SEL-SEL-BKK</route>
        <aircode>OZ</aircode>
        <depdate>2011-12-01</depdate>
        <arrdate>2011-12-01</arrdate>
        <deptime>1240</deptime>
        <arrtime>2210</arrtime>
        <transfer>1</transfer>
        <seat>Y</seat>
      </deproute>
       <arrroute>
        <route />
        <aircode />
        <depdate />
        <arrdate />
        <deptime />
        <arrtime />
        <transfer />
        <seat />
      </arrroute>
       <flightdatas>
         <segment>
          <dep>CAN</dep>
          <arr>SEL</arr>
          <air>OZ</air>
          <flightno>OZ370</flightno>
          <depairport>CAN</depairport>
          <arrairport>SEL</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>1240</deptime>
          <arrtime>1650</arrtime>
          <flytime>4.10</flytime>
          <plane>333</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
         <segment>
          <dep>SEL</dep>
          <arr>BKK</arr>
          <air>OZ</air>
          <flightno>OZ741</flightno>
          <depairport>SEL</depairport>
          <arrairport>BKK</arrairport>
          <depdate>2011-12-01</depdate>
          <arrdate>2011-12-01</arrdate>
          <deptime>1820</deptime>
          <arrtime>2210</arrtime>
          <flytime>3.50</flytime>
          <plane>333</plane>
          <seat>Y</seat>
          <stop>0</stop>
          <datas />
        </segment>
      </flightdatas>
      <rulestr>1F9B1B40B88E233815BA630DF7CAE2C4</rulestr>
    </flight>
  </flights>
</lists>
  XmlNodeList nl = dom.SelectNodes("lists/flights/flight");
                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("depdate", typeof(String)));
XmlNode node;
                DataRow dr;
                for (int i = 0; i < nl.Count; i++)
                {
                    node = nl.Item(i);
                    dr = dt.NewRow();
                    dr["depdate"] = node["deproute"]["depdate"].InnerText;
 dt.Rows.Add(dr);

                }
                this.Repeater1.DataSource = dt;
                this.Repeater1.DataBind();

<asp:Repeater ID="Repeater1" runat="server"><ItemTemplate>
                    <tr> <td><%#Eval("depdate")%></td></tr>
<asp:Repeater ID="Repeater2" runat="server"><ItemTemplate>
<tr><td>读取flightdatas/segment中的详细</td></tr></ItemTemplate></asp:Repeater></ItemTemplate></asp:Repeater>