如何绑定XML文件使用XML数据源为DropDownList? [英] How do I bind xml file to dropdownlist using XML datasource?

查看:166
本文介绍了如何绑定XML文件使用XML数据源为DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何绑定XML文件到ASP.NET的DropDownList使用的XmlDataSource?如果我不喜欢它下面,我看空的DropDownList。

ASP.NET

 < ASP:DropDownList的=服务器ID =ddlDEMODataValueField =BILLABDataTextField =BILLAB的DataSourceID =xdsDemo>
< / ASP:DropDownList的>< ASP:的XmlDataSource ID =xdsDemo=服务器数据文件=〜/ XML / Bills.xml
                        XPath的=/票据/比尔>< / ASP:&的XmlDataSource GT;

XML:

 <?XML版本=1.0编码=UTF-8&GT?;
<票据>
  <比尔 - GT;
    &所述; BILLID→1&下; / BILLID>
    < BILLAB>吲; / BILLAB>
  < /比尔及GT;
< /票据>


解决方案

据工作属性,而不是元素。这将有工作,如果你的XML看起来就像是:

 <?XML版本=1.0编码=UTF-8&GT?;
<票据>
  &所述;比尔BILLID =1BILLAB =ONE>
  < /比尔及GT;
< /票据>

您可以使用转换来解决它。看这里:
<一href=\"http://kanakaiah.word$p$pss.com/2008/05/06/using-xslt-files-with-the-new-xmldatasource-control/\" rel=\"nofollow\">http://kanakaiah.word$p$pss.com/2008/05/06/using-xslt-files-with-the-new-xmldatasource-control/

基于该链接的解决方案,你应该写XSL像

 &LT;的xsl:样式版本=1.0的xmlns:XSL =htt​​p://www.w3.org/1999/XSL/Transform&GT;
  &LT;的xsl:模板匹配=法案&GT;
    &LT;票据&GT;
      &LT; XSL:申请模板选择=条例/&GT;
    &LT; /票据&GT;
  &LT; / XSL:模板&GT;
  &LT;的xsl:模板匹配=条例&GT;
    &LT;比尔 - GT;
      &LT;的xsl:属性名称=BILLID&GT;
        &LT; XSL:选择=BILLID/&GT值的;
      &LT; / XSL:属性&GT;
      &LT;的xsl:属性名称=BILLAB&GT;
        &LT; XSL:选择=BILLAB/&GT值的;
      &LT; / XSL:属性&GT;
    &LT; / BILL&GT;
  &LT; / XSL:模板&GT;
&LT; / XSL:样式&GT;

How to bind xml file to asp.net dropdownlist using xmldatasource? If I do it like below, I see empty dropdownlist.

ASP.NET

<asp:DropDownList runat="server" ID="ddlDEMO"  DataValueField="BILLAB" DataTextField="BILLAB" DataSourceID="xdsDemo">
</asp:DropDownList> 

<asp:XmlDataSource ID="xdsDemo" runat="server" DataFile="~/XML/Bills.xml" 
                        XPath="/Bills/Bill"></asp:XmlDataSource>

XML:

<?xml version="1.0" encoding="utf-8" ?>
<Bills>
  <Bill>
    <BILLID>1</BILLID>
    <BILLAB>ONE</BILLAB>
  </Bill>
</Bills>

解决方案

It is working for attributes, not elements. This would have work if your XML looked like that:

<?xml version="1.0" encoding="utf-8" ?>
<Bills>
  <Bill BILLID="1" BILLAB="ONE">
  </Bill>
</Bills>

You can use transformation to fix it. Look here: http://kanakaiah.wordpress.com/2008/05/06/using-xslt-files-with-the-new-xmldatasource-control/

Based on the solution in that link you should write xsl like that:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="Bills">
    <Bills>
      <xsl:apply-templates select="Bill"/>
    </Bills>
  </xsl:template>
  <xsl:template match="Bill">
    <Bill>
      <xsl:attribute name="BILLID">
        <xsl:value-of select="BILLID"/>
      </xsl:attribute>
      <xsl:attribute name="BILLAB">
        <xsl:value-of select="BILLAB"/>
      </xsl:attribute>
    </BILL>
  </xsl:template>
</xsl:stylesheet>

这篇关于如何绑定XML文件使用XML数据源为DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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