asp.net xml 增删改操作

  1 user.xml
  2 <?xml version="1.0" encoding="utf-8"?>
  3 <user>
  4   <person>
  5   </person>
  6   <person name="风拉" sex="" age="25">
  7     <pass>123</pass>
  8     <Address>大明</Address>
  9   </person>
 10   <person name="风云" sex="" age="25">
 11     <pass>123</pass>
 12   </person>
 13   <person name="" sex="" age="25">
 14     <pass>123</pass>
 15     <Address>松江区</Address>
 16   </person>
 17 </user>
 18 
 19 1, private XmlDocument xmlDoc;
 20 
 21 
 22     //添加
 23     private void InsertElement()
 24     {
 25 
 26         XmlNode xmldocselect = xmlDoc.SelectSingleNode("user");
 27         XmlElement el = xmlDoc.CreateElement("person");
 28         el.SetAttribute("name", "风云");
 29         el.SetAttribute("sex", "");
 30         el.SetAttribute("age", "25");
 31         XmlElement xesub1 = xmlDoc.CreateElement("pass");
 32         xesub1.InnerText = "123";
 33         el.AppendChild(xesub1);
 34         xmldocselect.AppendChild(el);
 35         XmlElement xx = xmlDoc.CreateElement("Address");
 36         xx.InnerText = "松江区";
 37         el.AppendChild(xx);
 38         xmlDoc.Save(Server.MapPath("user.xml"));
 39     }
 40     //修改
 41     private void UpdateElement()
 42     {
 43         LoadXml();
 44         XmlNodeList nodeList = xmlDoc.SelectSingleNode("user").ChildNodes;
 45         foreach (var item in nodeList)
 46         {
 47             XmlElement xe = (XmlElement)item;
 48             if (xe.GetAttribute("name") == "风云")
 49             {
 50                 xe.SetAttribute("name", "发明");
 51 
 52                 //如果下面有子节点在下走
 53                 XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
 54                 foreach (XmlNode xn1 in nls)//遍历
 55                 {
 56                     XmlElement xe2 = (XmlElement)xn1;//转换类型
 57                     if (xe2.Name == "pass")//如果找到
 58                     {
 59                         xe2.InnerText = "66666";//则修改
 60                         break;
 61 
 62                     }
 63                 }
 64 
 65                 break;
 66             }
 67         }
 68           xmlDoc.Save(Server.MapPath("user.xml"));//保存
 69 
 70 
 71 
 72     }
 73 
 74     //删除
 75     private void DeleteElement()
 76     {
 77         LoadXml();
 78         XmlNodeList nodeList = xmlDoc.SelectSingleNode("user").ChildNodes;
 79         foreach (var item in nodeList)
 80         {
 81             XmlElement xe = (XmlElement)item;
 82             if (xe.GetAttribute("name") == "发明")
 83             {
 84                 xe.RemoveAll();
 85 
 86                 ////如果下面有子节点在下走
 87                 //XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点
 88                 //foreach (XmlNode xn1 in nls)//遍历
 89                 //{
 90                 //    XmlElement xe2 = (XmlElement)xn1;//转换类型
 91                 //    if (xe2.Name == "pass")//如果找到
 92                 //    {
 93                 //        xe2.InnerText = "66666";//则修改
 94                 //        break;
 95 
 96                 //    }
 97                 //}
 98 
 99                 break;
100             }
101         }
102         xmlDoc.Save(Server.MapPath("user.xml"));//保存
103 
104     }
105 
106     //显示
107     private void showIt()
108     {
109         LoadXml();
110         XmlNode xn = xmlDoc.SelectSingleNode("user");
111         XmlNodeList xnl = xn.ChildNodes;
112         foreach (var item in xnl)
113         {
114             XmlElement xe=(XmlElement)item;
115             Response.Write(xe.GetAttribute("name"));
116         }
117     }
118     //查找xml
119     private void LoadXml()
120     {
121         xmlDoc = new XmlDocument();
122         xmlDoc.Load(Server.MapPath("User.xml"));
123     }
124 
125 
126 
127 adminFunction.xml
128 
129 <?xml version="1.0" encoding="utf-8" ?>
130 <functions>
131   <function name="商品发布" url="~/page/leaves/leave.aspx">
132     <subfunction name="添加商品" url="~/page/leaves/leave.aspx">
133     </subfunction>
134     <subfunction name="添加商品" url="~/page/leaves/leave.aspx">
135     </subfunction>
136   </function>
137   <function name="留言发布" url="~/page/leaves/leave.aspx">
138     <subfunction name="添加留言" url="~/page/leaves/leave.aspx">
139     </subfunction>
140     <subfunction name="修改留言" url="~/page/leaves/leave.aspx">
141     </subfunction>
142   </function>
143 </functions>
144 
145 
146 
147     private void InitFunctions()
148     {
149         XmlDocument xmldoc = new XmlDocument();
150         xmldoc.Load(Server.MapPath("adminFunction.xml"));
151         XmlNode root = xmldoc.DocumentElement;
152         tvFunction.Nodes.Add(CreateNode(root,new TreeNode()));
153     }
154 
155     //递归
156     /// <summary>
157     /// 
158     /// </summary>
159     /// <param name="node">根节点</param>
160     /// <param name="tn">根节点对应的树节点</param>
161     /// <returns></returns>
162     private TreeNode CreateNode(XmlNode root, TreeNode tn)
163     {
164 
165         foreach (XmlNode node in root.ChildNodes)
166         {
167             XmlElement subxmlEmt = node as XmlElement;
168             string subname = subxmlEmt.GetAttribute("name");
169             string suburl = subxmlEmt.GetAttribute("url");
170             TreeNode subtn = new TreeNode();
171             subtn.Text = subname;
172             subtn.NavigateUrl = suburl;
173             if (node.HasChildNodes)
174             {
175                 subtn.ChildNodes.Add(CreateNode(node, subtn));
176             }
177             tn.ChildNodes.Add(subtn);
178         }
179         return tn;
180 
181     }
182 
183 
184     //读实例
185     private void ReadEx()
186     {
187         string path1 = Server.MapPath("TextFile.txt");
188         if (File.Exists(path1))
189         {
190             StreamReader reader = new StreamReader(path1, System.Text.Encoding.GetEncoding("gb2312"));
191 
192                 string str = reader.ReadToEnd();
193                 Response.Write((str));
194                 reader.Close();
195         }
196 
197         WriteFile(path1,"vvvvv");
198     }
199     //写文件
200     /// </summary>
201     /// <param name="Path">文件路径</param>
202     /// <param name="Strings">文件内容</param>
203     public static void WriteFile(string Path, string Strings)
204     {
205        
206         if (!System.IO.File.Exists(Path))
207         {
208             System.IO.FileStream f = System.IO.File.Create(Path);
209             f.Close();
210         }
211         System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, false, System.Text.Encoding.GetEncoding("gb2312"));
212         f2.Write(Strings);
213         f2.Close();
214         f2.Dispose();
215     }

 

asp.net xml 增删改操作,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。