zoukankan      html  css  js  c++  java
  • linq对xml的增删查改

    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Collections.Generic;
    
    
    public partial class _Default : System.Web.UI.Page
    {
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
    
            if (!IsPostBack)
            {
                //调用自定义bindDl方法绑定下拉列表
                bindDl();
            }
    
        }
    
        protected XElement createGoods(string id, string name, string price)
        {
            //创建XML文件元素
            XElement xeGoods = new XElement("goodsID",
                  new XAttribute("ID", id),
                  new XElement("goodsName", name),
                  new XElement("goodsPrice", price));
            //返回XElement对象
            return xeGoods;
        }
    
        protected void btnAddxml_Click(object sender, EventArgs e)
        {
            //获取XML文件的路径
            string xmlFilePath = Server.MapPath("goods.xml");
            //加载XML文件
            XElement xe = XElement.Load(xmlFilePath);
            //调用自定义createGoods方法添加商品
            xe.Add(createGoods(txtID.Text, txtName.Text, txtPrice.Text));
            //保存添加后的XML文件
            xe.Save(xmlFilePath);
            RegisterStartupScript("", "<script>alert('添加成功!')</script>");
        }
    
        protected void bindDl()
        {
            //获取XML文件的
            string xmlFilePath = Server.MapPath("goods.xml");
            //加载XML文件
            XElement xel = XElement.Load(xmlFilePath);
            //查询goodsID
            IEnumerable<XElement> elements = from e in xel.Elements("goodsID")
                                             select e;
            //遍历查询后的结果
            foreach (XElement xe in elements)
            {
                //创建ListItem对象
                ListItem li = new ListItem();
                //设置显示文本
                li.Text = xe.Attribute("ID").Value;
                //设置值
                li.Value = xe.Attribute("ID").Value;
                //将LIstItem对象添加到下拉列表中
                DropDownList1.Items.Add(li);
            }
    
        }
    
        protected void bindInfo(string strId)
        {
            //获取XML文件
            string xmlFilePath = Server.MapPath("goods.xml");
            //加载XML文件
            XElement xel = XElement.Load(xmlFilePath);
            //查询指定ID的数据
            IEnumerable<XElement> elements = from e in xel.Elements("goodsID")
                                             where e.Attribute("ID").Value == strId
                                             select e;
            //遍历查询后的结果
            foreach (XElement xe in elements)
            {
                //将商品信息显示在文本框中
                txtSId.Text = xe.Attribute("ID").Value;
                txtSName.Text = xe.Element("goodsName").Value;
                txtSPrice.Text = xe.Element("goodsPrice").Value;
    
    
            }
        }
    
        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //获取当前选择的编号
            string strId = DropDownList1.SelectedValue;
            //调用自定义bindInfo方法显示指定编号的商品信息
            bindInfo(strId);
        }
    
        protected void btnSet_Click(object sender, EventArgs e)
        {
            //获取XML文件的路径
            string xmlFilePath = Server.MapPath("goods.xml");
            //加载XML文件
            XElement xel = XElement.Load(xmlFilePath);
            //查询指定编号的商品
            IEnumerable<XElement> element = from el in xel.Elements("goodsID")
                                            where el.Attribute("ID").Value == DropDownList1.SelectedValue
                                            select el;
            //获取第一个元素
            XElement result = element.First();
           
            //替换节点中的文本
            result.ReplaceNodes(
                new XElement("goodsName", txtSName.Text),
                new XElement("goodsPrice", txtSPrice.Text)
                );
            //保存XML文件
            xel.Save(xmlFilePath);
        }
    }
    

      网页上显示xml:

     protected void Page_Load(object sender, EventArgs e)
        {
            //获取XML文件的路径
            string xmlFilePath = Server.MapPath("goods.xml");
            //加载XML文件
            XDocument doc = XDocument.Load(xmlFilePath);        
            //输出XML文件
            Response.Write(doc);
            //设置HTTP MIME类型
            Response.ContentType = "text/xml";
            Response.End();
        }
    

      详细代码解释在http://download.csdn.net/detail/yekeyishuo/5037848

  • 相关阅读:
    python网络编程 — HTTP客户端
    实验吧Web-天网管理系统
    实验吧Web-FALSE
    实验吧Web-Forms
    离散数学-集合运算基本法则
    sublime text3编译运行C,Java程序的一些配置
    kali Rolling 安装QQ和虚拟机
    python并行任务之生产消费模式
    Linux磁盘管理
    python网络编程之网络主机信息
  • 原文地址:https://www.cnblogs.com/YEKEYISHUO/p/2881200.html
Copyright © 2011-2022 走看看