zoukankan      html  css  js  c++  java
  • Linq to XML

    .Net中的System.Xml.Linq命名空间提供了linq to xml的支持。这个命名空间中的XDocument,XElement以及XText,XAttribute提供了读写xml文档的关键方法。
    1. 使用linq to xml写xml:
    使用XDocument的构造函数可以构造一个Xml文档对象;使用XElement对象可以构造一个xml节点元素,使用XAttribute构造函数可以构造元素的属性;使用XText构造函数可以构造节点内的文本。
    代码如下:(本代码是按钮的点击处理程序)
    protected void btnGenerateXml_Click(object sender, EventArgs e)
        {
            if (File.Exists(Server.MapPath("~/xml/stus.xml")))
            {
                ClientScript.RegisterStartupScript(this.GetType(),"","alert('xml文件已经存在!')",true);
                return;
            }
            var xDoc = new XDocument(new XElement("students",
               new XElement("student",
                   new XElement("name", "张三"),
                   new XElement("age", 23),
                   new XAttribute("num", "S001")),
               new XElement("student",
                   new XElement("name", "李四"),
                   new XElement("age", 24),
                   new XAttribute("num", "S002")),
               new XElement("student",
                   new XElement("name", "王五"),
                   new XElement("age", 25),
                   new XAttribute("num", "S003"))));
    
            //xDoc输出xml的encoding是系统默认编码,对于简体中文操作系统是gb2312
            //默认是缩进格式化的xml,而无须格式化设置
            xDoc.Save(Server.MapPath("~/xml/stus.xml"));
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('xml文件创建成功!')", true);
        }

    通过上面的代码能再xml文件夹中创建一个stus.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <students>
      <student num="S001">
        <name>张三</name>
        <age>23</age>
      </student>
      <student num="S002">
        <name>李四</name>
        <age>24</age>
      </student>
      <student num="S003">
        <name>王五</name>
        <age>25</age>
      </student>
    </students>

    2. 使用linq to xml 读取xml并在Reapter控件中显示相应数据

    代码如下:

    protected void btnShowXml_Click(object sender, EventArgs e)
        {
            ReapterBind();
        }
        public void ReapterBind()
        {
            var xDoc = XDocument.Load(Server.MapPath("~/xml/stus.xml"));
            var result = from item in xDoc.Element("students").Elements()
                        where Convert.ToInt32(item.Element("age").Value) > 23
                        select new
                        {
                            StuNum = item.Attribute("num").Value,
                            StuName=item.Element("name").Value,
                            StuAge = item.Element("age").Value
                        };
            this.rptStudents.DataSource = result;
            this.rptStudents.DataBind();
        }

    aspx中的代码如下:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="LinqToXmlTest.aspx.cs" Inherits="LinqToXmlTest" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnGenerateXml" runat="server" Text="生成XML" OnClick="btnGenerateXml_Click" />
            <asp:Button ID="btnShowXml" runat="server" Text="加载XML" Height="21px" OnClick="btnShowXml_Click" />
            <br />
            -----------------------------
            <table border="1">
                <tr>
                    <th>
                        学号
                    </th>
                    <th>
                        姓名
                    </th>
                    <th>
                        年龄
                    </th>
                </tr>
                <asp:Repeater ID="rptStudents" runat="server">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%#Eval("StuNum") %>
                            </td>
                            <td>
                                <%#Eval("StuName") %>
                            </td>
                            <td>
                                <%#Eval("StuAge") %>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
            </table>
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    MySQL 你好,死锁
    Python+Scrapy+Selenium数据采集
    令牌桶限频(Token Bucket)
    Go 逃逸分析
    ElasticSearch 连载二 中文分词
    ElasticSearch 连载一 基础入门
    基于GitLab CI搭建Golang自动构建环境
    Go 性能分析之案例一
    MySQL InnoDB 行记录格式(ROW_FORMAT)
    MySQL InnoDB 逻辑存储结构
  • 原文地址:https://www.cnblogs.com/tianguook/p/3928228.html
Copyright © 2011-2022 走看看