C#操作bcastr.xml代码
- 发表于
- 前端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
//读取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代码
所有媒体,可在保留署名、
原文连接
的情况下转载,若非则不得使用我方内容。