zoukankan      html  css  js  c++  java
  • Linq To Xml基础

     1 <?xml version="1.0" encoding="utf-8" ?>
     2 <Books>
     3   <Category Order="1" Name="文学类">
     4     <Book Order="1" Name="战争与和平" Author="张三" Price="120.00" Publish="北京大学出版社"></Book>
     5     <Book Order="2" Name="简爱" Author="李四" Price="153.00" Publish="清华大学出版社"></Book>
     6     <Book Order="3" Name="罪与罪" Author="王五" Price="89.00" Publish="工业出版社"></Book>
     7   </Category>
     8 
     9   <Category Order="3" Name="计算机类">
    10     <Book Order="1" Name="Linq高级编程" Author="童贝" Price="67.40" Publish="北京大学出版社"></Book>
    11     <Book Order="2" Name="深入浅出WPF" Author="向刚" Price="65.00" Publish="清华大学出版社"></Book>
    12     <Book Order="3" Name="ASP.Net MVC4" Author="马六" Price="89.00" Publish="工业出版社"></Book>
    13   </Category>
    14 
    15   <Category Order="2" Name="科技类">
    16     <Book Order="1" Name="自然" Author="邵叶" Price="130.40" Publish="北京大学出版社"></Book>
    17     <Book Order="2" Name="新工业革命" Author="李安" Price="65.00" Publish="清华大学出版社"></Book>
    18     <Book Order="3" Name="时空" Author="马六" Price="150.00" Publish="工业出版社"></Book>
    19   </Category>
    20 </Books>
     1 private void LoadXml()
     2     {
     3         XElement bookXml = XElement.Load(Server.MapPath("~/Book.xml"));
     4 
     5         if (bookXml == null)
     6         {
     7             return;
     8         }
     9 
    10         // 查找所有书籍分类
    11         var categorysElement = from item in bookXml.Elements("Category")
    12                                orderby (int.Parse(item.Attribute("Order").Value))
    13                                select item;
    14         List<Book> listBook = new List<Book>();
    15 
    16         foreach (var categoryElement in categorysElement)
    17         {
    18             // 获取书籍分类的名称
    19             string categoryName = categoryElement.Attribute("Name").Value;
    20 
    21             // 查找此分类下的所有书籍
    22             var booksElement = from book in categoryElement.Elements("Book")
    23                                orderby (int.Parse(book.Attribute("Order").Value))
    24                                select book;
    25 
    26             foreach (var bookElement in booksElement)
    27             {
    28                 Book book = new Book();
    29                 book.Category = categoryName;
    30                 book.Name = bookElement.Attribute("Name").Value;
    31                 book.Author = bookElement.Attribute("Author").Value;
    32                 book.Price = bookElement.Attribute("Price").Value;
    33                 book.Publish = bookElement.Attribute("Publish").Value;
    34                 listBook.Add(book);
    35             }
    36         }
    37 
    38         this.grdBooks.DataSource = listBook;
    39         this.grdBooks.DataBind();
    40     }
    41 
    42 
    43     public class Book
    44     {
    45         /// <summary>
    46         /// 分类名称
    47         /// </summary>
    48         public string Category { get; set; }
    49 
    50         /// <summary>
    51         /// 书籍名称
    52         /// </summary>
    53         public string Name { get; set; }
    54 
    55         /// <summary>
    56         /// 价格
    57         /// </summary>
    58         public string Price { get; set; }
    59 
    60         /// <summary>
    61         /// 作者
    62         /// </summary>
    63         public string Author { get; set; }
    64 
    65         /// <summary>
    66         /// 出版社
    67         /// </summary>
    68         public string Publish { get; set; }
    69     }
     1 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
     2 
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     4 
     5 <html xmlns="http://www.w3.org/1999/xhtml">
     6 <head runat="server">
     7     <title></title>
     8 </head>
     9 <body>
    10     <form id="form1" runat="server">
    11     <div>
    12         <asp:GridView AutoGenerateColumns="false" ID="grdBooks" runat="server" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black" GridLines="None">
    13             <AlternatingRowStyle BackColor="PaleGoldenrod" />
    14             <Columns>
    15                 <asp:BoundField DataField="Category" HeaderText="分类" ItemStyle-Width="10%" />
    16                 <asp:BoundField DataField="Name" HeaderText="书名" ItemStyle-Width="20%" />
    17                 <asp:BoundField DataField="Author" HeaderText="作者" ItemStyle-Width="8%" />
    18                 <asp:BoundField DataField="Price" HeaderText="价格" ItemStyle-Width="8%" />
    19                 <asp:BoundField DataField="Publish" HeaderText="出版社" ItemStyle-Width="15%" />
    20             </Columns>
    21             <FooterStyle BackColor="Tan" />
    22             <HeaderStyle BackColor="Tan" Font-Bold="True" />
    23             <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
    24             <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    25             <SortedAscendingCellStyle BackColor="#FAFAE7" />
    26             <SortedAscendingHeaderStyle BackColor="#DAC09E" />
    27             <SortedDescendingCellStyle BackColor="#E1DB9C" />
    28             <SortedDescendingHeaderStyle BackColor="#C2A47B" />
    29 
    30         </asp:GridView>
    31     </div>
    32     </form>
    33 </body>
    34 </html>
  • 相关阅读:
    java中将表单转换为PDF
    base64图片
    ORACLE中用户等系统信息操作
    jquery中live is not a function的问题
    完全卸载Oracle11G
    jquery 获取鼠标和元素的坐标点
    JS的多线程
    Oracle和SQLServer解锁杀进程
    JAVA 通过LDAP获取AD域用户及组织信息
    oracle基础语法大全
  • 原文地址:https://www.cnblogs.com/2013likong/p/3494697.html
Copyright © 2011-2022 走看看