0

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>

2 Answers 2

2

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

Comments

0

I would either create a class or a structure and serialize/deserialize from/to xml to my class/struct. Here is how you serialize your xml documents:

http://support.microsoft.com/kb/815813

After this I create a generic List of my class/struct and it is much easier to bind to anything and manipulate in general.

Good luck.

1 Comment

This guy definitely knows what to do :). Using this strategy now.

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.