0

I have a xml file which is given to me by client so i can't change it it looks like this:

CodeBase.XML

<?xml version="1.0" encoding="utf-8"?>
<CATALOG>
  <orderByTitleDesc />
  <orderByTitleDesc />
  <orderByTitleDesc />
  <orderByTitleDesc />
  <orderByTitleDesc />
  <CD>
    <CodeBase>hellow</CodeBase>
  </CD>
  <CD>
    <CodeBase>http://KL1225K7/EViewer/Client/EvCtrl6En.cab#version=9,4,2,9</CodeBase>
  </CD>
  <CD>
    <CodeBase>31 amrch testing for font</CodeBase>
  </CD>
  <CD>
    <CodeBase>CodeBase2</CodeBase>
  </CD>
  <CD>
    <CodeBase>Ser</CodeBase>
  </CD>
  <CD>
    <CodeBase>ZaidiTest</CodeBase>
  </CD>
  <CD>
    <CodeBase>test222</CodeBase>
  </CD>
</CATALOG>

I want to bind this xml with this in ddl

I am writing the code in code behind file as follows:

 protected void Page_Load(object sender, EventArgs e)
    {

        string filePath = Server.MapPath("CodeBase.xml");
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(filePath);
            ddlCodeBase.DataSource = ds.Tables["1"];
            ddlCodeBase.DataTextField = "CodeBase";
            ddlCodeBase.DataValueField = "CD";
            ddlCodeBase.DataBind();
        }
    }

Client side code as Follows:

 <asp:DropDownList ID="ddlCodeBase" runat="server" ></asp:DropDownList>

But i am getting empty DDl (No data in it). Please Let me know Guys what to do. Thanks in Advance.

1

1 Answer 1

0

Try below code . its working fine for me.

 string filePath = Server.MapPath("~/Cities.xml");
using (DataSet ds = new DataSet())
{
    ds.ReadXml(filePath);
    ddlCities.DataSource = ds;
    ddlCities.DataTextField = "CodeBase";
    ddlCities.DataValueField = "CodeBase";
    ddlCities.DataBind();
}
Sign up to request clarification or add additional context in comments.

5 Comments

When I Debug till Last in gives me Error like this An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'CodeBase'.
Change file name i used cities.xml
protected void Page_Load(object sender, EventArgs e) { //fillDDL(); string filePath = Server.MapPath("~/CodeBase.xml"); //using (DataSet ds = new DataSet()) //{ DataSet ds = new DataSet(); ds.ReadXml(filePath); ddlCodeBase.DataSource = ds.Tables["CD"]; ddlCodeBase.DataTextField = "CodeBase"; ddlCodeBase.DataValueField = "CodeBase"; ddlCodeBase.DataBind(); // } }
By Adding this to my Code ddlCodeBase.DataSource = ds.Tables["CD"];
if my code helpful for you then voted me .@BasantGera

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.