加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Linq to xml

发布时间:2020-12-16 05:51:35 所属栏目:百科 来源:网络整理
导读:using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml.Linq;namespace WebApplication1{ public partial class WebForm2 : System.Web.UI.Page { prot
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            //Create();
            //GetAllXML();
            //ChangeXML();
            //Remove();
        }

        /// <summary>
        /// 删除XML
        /// </summary>
        public void Remove()
        {
            XDocument doc = XDocument.Load(Request.PhysicalApplicationPath + "test.xml");
            doc.Element("Struect").Elements("Struect222").Where(x => x.Element("Id").Value.Trim() == "0001").Remove();
            doc.Save(Request.PhysicalApplicationPath + "test.xml");
        }

        /// <summary>
        /// 插入XML
        /// </summary>
        public void ChangeXML()
        {
            XDocument doc = XDocument.Load(Request.PhysicalApplicationPath + "test.xml");
            XElement xml = new XElement(
                new XElement("Struect222",new XElement("Id","0001"),new XElement("Name","000string")
                ));

            doc.Element("Struect").Add(xml);
            doc.Save(Request.PhysicalApplicationPath + "test.xml");
        }

        /// <summary>
        /// 读取XML
        /// </summary>
        public void GetAllXML()
        {
            XDocument doc = XDocument.Load(Request.PhysicalApplicationPath + "test.xml");
            var status = (from x in doc.Descendants("Struect222")
                          where x.Element("Id").Value=="3"
                         select new
                         {
                             id = x.Element("Id").Value,Name = x.Element("Name").Value
                         });

            foreach (var val in status)
            {
                Response.Write(string.Format("{0}--{1}",val.id,val.Name));
            }
        }

        /// <summary>
        /// 创建XML
        /// </summary>
        public void Create()
        {
            List<Txt> list = new List<Txt>()
            {
                new Txt{ Id=1,Name="1string"},new Txt{ Id=2,Name="2string"},new Txt{ Id=3,Name="3string"},};
            XElement xml = new XElement("Struect",from x in list
                select
                new XElement("Struect222",x.Id),x.Name)));

            xml.Save(Request.PhysicalApplicationPath + "test.xml");
        }

        public struct Txt
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        protected void rpt_ItemCreated(object sender,RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

            if (e.Item.FindControl("tbA") == null || e.Item.FindControl("tbB") == null) return;

            TextBox tbA = e.Item.FindControl("tbA") as TextBox;
            TextBox tbB = e.Item.FindControl("tbB") as TextBox;
            tbA.TextChanged += tbA_TextChanged;
            tbB.TextChanged += tbB_TextChanged;
        }

        public void tbA_TextChanged(object sender,EventArgs e)
        {
            TextBox tbA = sender as TextBox;
            RepeaterItem rptA = (RepeaterItem)tbA.Parent;
            TextBox tbB = rptA.FindControl("tbB") as TextBox;
            Label lbC = rptA.FindControl("lableC") as Label;

            lbC.Text = (Convert.ToDecimal(tbA.Text.Trim()) * Convert.ToDecimal(tbB.Text.Trim())).ToString();
        }

        public void tbB_TextChanged(object sender,EventArgs e)
        {
            TextBox tbB = sender as TextBox;
            RepeaterItem rptB = (RepeaterItem)tbB.Parent;
            TextBox tbA = rptB.FindControl("tbA") as TextBox;
            Label lbC = rptB.FindControl("lableC") as Label;

            lbC.Text = (Convert.ToDecimal(tbA.Text.Trim()) * Convert.ToDecimal(tbB.Text.Trim())).ToString();
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读