Click here to Skip to main content
15,905,316 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
here is my code


  protected void BindDataList()
    {
        //------------------------------Retrive all Albums from XML ------------------------//
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
        XmlNodeList nodelist = xmldoc.GetElementsByTagName("Album");
        ArrayList listItems = new ArrayList();
        //fetch all albums in XML
        for (int i = 0; i < nodelist.Count; i++)
        {
            string anode = nodelist[i].Attributes["Name"].Value;

            listItems.Add(anode);
        }
        dtlist.DataSource = listItems;
        dtlist.DataBind();

     }

//aspx page(design page)
    <asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
                    <itemtemplate>
                      <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%#((System.Xml.XmlNode)Container.DataItem).Attributes["Name"].Value %>' Text='<%#((System.Xml.XmlNode)Container.DataItem).Attributes["Name"].Value%>'>
                       
                    </itemtemplate>



while debugging its shows me error in desgin view for linkbutton
// This is the error i get
Unable to cast object of type 'System.String' to type 'System.Xml.XmlNode'.
//where i am going wrong, can u please help
Posted
Updated 1-Feb-14 0:04am
v5
Comments
JoCodes 1-Feb-14 3:01am    
CommandArgument and Text should be a type of string .So just pass Value string instead of XMlnode type

1 solution

This give proper output...


C#
protected void BindDataList()
{


DataTable dt = new DataTable();
     
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(Server.MapPath("~/" + "XML/album.xml"));
        XmlNodeList nodelist = xmldoc.GetElementsByTagName("Album");
        ArrayList listItems = new ArrayList();
        dt.Columns.Add("Name", typeof(string));
        //fetch all albums in XML

        for (int i = 0; i < nodelist.Count; i++)
        {
            DataRow dtrow = dt.NewRow();
            dtrow["Name"] = nodelist[i].Attributes["Name"].Value;
          
            dt.Rows.Add(dtrow);
        }
       
        dtlist.DataSource = dt;
        dtlist.DataBind();
}

// aspx page

XML
<asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
                <ItemTemplate>

                <asp:LinkButton ID="LinkButton3" runat="server" CommandArgument='<%#Eval("Name") %>' Text='<%#Eval("Name") %>'></asp:LinkButton>

                </ItemTemplate>
                <ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px" HorizontalAlign="Center" VerticalAlign="Bottom" />
            </asp:DataList>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900