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",
new XElement("Id", x.Id),
new XElement("Name", 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();
}
}
}
Linq to xml
最新推荐文章于 2024-08-12 10:57:05 发布