本文介绍了如何使用Linq:使用C#.NET中的数据集加载XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Linq加载。



代码背后:



How to load using Linq.

Code Behind:

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 :





HTML :

<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文件:



< 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>

<作者>

< au_id> 213-46-8915< / au_id>

< au_lname>绿< / au_lname>

< au_fname> Margie< / au_fname>

< phone> 415 986-7020< / phone>

< address> 309 63rd St.#411< / address>

< city>奥克兰< / city>

< state> CA< / state>

< zip> 94618< / zip>

< ;合同> 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>圣何塞< / 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>




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>

推荐答案

Use the following code







       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;
       }


这篇关于如何使用Linq:使用C#.NET中的数据集加载XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 14:27