Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to load using Linq.

Code Behind:

C#
protected void Page_Load(object sender, EventArgs e)
    {
        string filePath = Server.MapPath("employee.xml");
        DataSet AuthorsDataSet = null;
        try
        {
           AuthorsDataSet = new DataSet();
            AuthorsDataSet.ReadXml(filePath);

            myGrid.DataSource = AuthorsDataSet;
            myGrid.DataMember = "authors";
            myGrid.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            //clearing the memory
            if (AuthorsDataSet != null)
                AuthorsDataSet = null;
        }
    }




HTML :
XML
<asp:GridView ID="myGrid" runat="server"
           AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White"
           HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
        <Columns>
               <asp:BoundField HeaderText="Employee Name" DataField="au_id" />
               <asp:BoundField HeaderText="Employee ID"
                   DataField="au_lname" ItemStyle-HorizontalAlign="Right" />
           </Columns>
   </asp:GridView>



XML file:

<Authors_Table>
<authors>
<au_id>172-32-1176</au_id>
<au_lname>White</au_lname>
<au_fname>Johnson</au_fname>
<phone>408 496-7223</phone>
<address>10932 Bigge Rd.</address>
<city>Menlo Park</city>
<state>CA</state>
<zip>94025</zip>
<contract>true</contract>
</authors>
<authors>
<au_id>213-46-8915</au_id>
<au_lname>Green</au_lname>
<au_fname>Margie</au_fname>
<phone>415 986-7020</phone>
<address>309 63rd St. #411</address>
<city>Oakland</city>
<state>CA</state>
<zip>94618</zip>
<contract>true</contract>
</authors>
<authors>
<au_id>238-95-7766</au_id>
<au_lname>Carson</au_lname>
<au_fname>Cheryl</au_fname>
<phone>415 548-7723</phone>
<address>589 Darwin Ln.</address>
<city>Berkeley</city>
<state>CA</state>
<zip>94705</zip>
<contract>true</contract>
</authors>
<authors>
<au_id>267-41-2394</au_id>
<au_lname>Hunter</au_lname>
<au_fname>Anne</au_fname>
<phone>408 286-2428</phone>
<address>22 Cleveland Av. #14</address>
<city>San Jose</city>
<state>CA</state>
<zip>95128</zip>
<contract>true</contract>
</authors>
<authors>
<au_id>274-80-9391</au_id>
<au_lname>Straight</au_lname>
<au_fname>Dean</au_fname>
<phone>415 834-2919</phone>
<address>5420 College Av.</address>
<city>Oakland</city>
<state>CA</state>
<zip>94609</zip>
<contract>true</contract>
</authors>
</Authors_Table>
Posted
Updated 8-Feb-15 22:25pm
v2

1 solution

Use the following code


C#
       public class authors
       {
           public string au_id { get; set; }
           public string au_lname { get; set; }
           public string au_fname { get; set; }
           public string phone { get; set; }
           public string address { get; set; }
           public string city { get; set; }
           public string state { get; set; }
           public string zip { get; set; }
           public bool contract { get; set; }
       }

private void btnLoad_Click(object sender, EventArgs e)
       {
           string filename = AppDomain.CurrentDomain.BaseDirectory + "employee.xml";//use your filepath
           var xmlresult = XDocument.Load(filename);

           var authorlist = from au in xmlresult.Descendants("Authors_Table").Descendants("authors")
                            select
                                new
                                {
                                    au_id = au.Element("au_id").Value,
                                    au_lname = au.Element("au_lname").Value,
                                    au_fname = au.Element("au_fname").Value,
                                    phone = au.Element("phone").Value,
                                    address = au.Element("address").Value,
                                    city = au.Element("city").Value,
                                    state = au.Element("state").Value,
                                    zip = au.Element("zip").Value,
                                    contract = au.Element("contract").Value

                                };

           List<authors> lstauthors = new List<authors>();

           foreach (var author in authorlist)
           {
               authors a_author = new authors();
               a_author.au_id = author.au_id;
               a_author.au_lname = author.au_lname;
               a_author.au_fname = author.au_fname;
               a_author.phone = author.phone;
               a_author.address = author.address;
               a_author.city = author.city;
               a_author.state = author.state;
               a_author.zip = author.zip;
               a_author.contract = (author.contract=="true")?true:false;
               lstauthors.Add(a_author);
           }
           dataGridView1.DataSource = lstauthors;
       }
 
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