如何在gridview的帮助下操作xml文件。我无法将gridview转换为添加,更新和删除操作并处理它们。
enter code here
[1]:http://i.stack.imgur.com/Me0Fb.png
protected void Page_Load(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load("d:/myXml.xml");//Adding an element
[enter image description here][1]
SerDes obj = new SerDes();
var cat = obj.DeSerialize(xmlDoc.ToString());
List<Item> items = cat.Items.ToList();
items.Add(new Item { ItemID = 2, ItemName = "Test", ItemPrice = 100, ItemQtyInStock = 50 });// adding an item
//cat.Items.ToList().FirstOrDefault(item => item.ItemID == 2).ItemName = "Custom Name"; //updating an item
var x = cat.Items.ToList().FirstOrDefault(item => item.ItemID == 2); //deleting an item
items.Remove(x);
var len = items.Count();
cat.Items = items.ToArray();
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(obj.Serialization(cat));
xdoc.Save(@"D:\myXml.xml");
}
protected void Button1_Click(object sender, EventArgs e)
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(@"D:\myXml.xml");
GridView1.DataSource = dataSet.Tables[2];
GridView1.DataBind();
}