C#操作bcastr.xml代码
- 发表于
- 前端
//读取xml中内容 private DataSet GetDataSet() { DataSet ds = new DataSet(); DataTable dataTable = new DataTable("Users"); dataTable.Columns.Add("item_url"); dataTable.Columns.Add("link"); dataTable.Columns.Add("itemtitle"); try { XmlDocument doc = new XmlDocument(); doc.Load(Server.MapPath("xmlpath")); if (doc == null) { return null; } XmlNodeList nodeList = doc.SelectNodes("/bcaster/item"); foreach (XmlNode node in nodeList) { DataRow row = dataTable.NewRow(); row["item_url"] = node.Attributes["item_url"].Value; row["link"] = node.Attributes["link"].Value; row["itemtitle"] = node.Attributes["itemtitle"].Value; dataTable.Rows.Add(row); } } catch (Exception ex) { throw new Exception(ex.Message, ex); } ds.Tables.Add(dataTable); return (ds); } ///绑定gridview private void Binddata() { string file = Server.MapPath("~/bcastr.xml"); XmlTextReader reader = new XmlTextReader(file); DataTable dt = new DataTable("bcaster"); dt.Columns.Add(new DataColumn("item_url", typeof(string))); dt.Columns.Add(new DataColumn("link", typeof(string))); dt.Columns.Add(new DataColumn("id", typeof(string))); reader.ReadStartElement();// MoveToElement(); while (reader.Read()) { DataRow dr = dt.NewRow(); if (reader.HasAttributes) { reader.MoveToFirstAttribute(); for (int i = 0; i < reader.AttributeCount; i++) { dr[reader.Name] = reader.Value; reader.MoveToNextAttribute(); } dt.Rows.Add(dr); } } reader.Close(); GridView1.DataSource = dt; GridView1.DataBind(); } //修改xml中的节点 XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.Load(Server.MapPath("xmlpaht")); int temp = e.Item.ItemIndex; temp = temp + 1; string id = temp.ToString(); XmlNodeList nodelist=xmldoc.SelectSingleNode("bcaster").ChildNodes;//获取item节点的所有子节点 foreach (XmlNode xn in nodelist) { XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("id")==id) { xe.SetAttribute("id", id); xe.SetAttribute("item_url",((TextBox)e.Item.FindControl("txtUrl")).Text.Trim()); xe.SetAttribute("link", ((TextBox)e.Item.FindControl("txtLink")).Text.Trim()); xe.SetAttribute("itemtitle", ((TextBox)e.Item.FindControl("txtTitle")).Text.Trim()); } } xmldoc.Save(Server.MapPath("xmlpath")); //添加内容 XmlDataDocument xmldoc = new XmlDataDocument(); xmldoc.Load(Server.MapPath("xmlpath")); XmlNode root = xmldoc.SelectSingleNode("bcaster");//查找<bcaster> XmlElement xe1 = xmldoc.CreateElement("item");//创建一个<Node>节点 XmlNodeList nodelist = xmldoc.SelectNodes("/bcaster/item"); int count = nodelist.Count + 1; string id=count.ToString(); xe1.SetAttribute("id",id); xe1.SetAttribute("item_url",((TextBox)Panel1.FindControl("txtUrl")).Text.Trim()); xe1.SetAttribute("link", ((TextBox)Panel1.FindControl("txtLink")).Text.Trim()); xe1.SetAttribute("itemtitle", ((TextBox)Panel1.FindControl("txtTitle")).Text.Trim()); root.AppendChild(xe1); xmldoc.Save(Server.MapPath("xmlpath")); //删除其中内容 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("xmlpath")); XmlNode root = xmlDoc.SelectSingleNode("bcaster"); XmlNodeList xnl = xmlDoc.SelectSingleNode("bcaster").ChildNodes; int temp = e.Item.ItemIndex; temp = temp + 1; string id = temp.ToString(); for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute("id") == id) { root.RemoveChild(xe); if (i < xnl.Count) { i = i - 1; } } } xmlDoc.Save(Server.MapPath("xmlpath")); ///删除 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("~/bcastr.xml")); // XmlNodeList xnl = xmlDoc.SelectSingleNode("bcastr").ChildNodes; //获取行信息的字段count XmlNode root = xmlDoc.SelectSingleNode("bcaster");//查找root节点 XmlNodeList xnl = root.ChildNodes; //获取root下面所有子节点 foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; if (xe.GetAttribute("id") == id) //获取节点以及所有子集的串联值,这里的news_count1是count的值 { xe.RemoveAll(); break; } } xmlDoc.Save(Server.MapPath("~/bcastr.xml"));
原文连接:C#操作bcastr.xml代码
所有媒体,可在保留署名、
原文连接
的情况下转载,若非则不得使用我方内容。