C#操作Xml(增删改查)练习

原创 C#操作Xml(增删改查)练习

web.config配置:

  1. <appSettings>  
  2.   <add key="xmlFile" value="xml/class.xml"/>  
  3. </appSettings>  

  前台:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="test_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title>C#操作Xml(增删改查)练习</title>  
  8. </head>  
  9. <body>  
  10.     <form id="form1" runat="server">  
  11.     <div id="showXml" runat="server">  
  12.     显示Xml文档  
  13.     </div>  
  14.     <div style="background-color:Green;color:Yellow;" mce_style="background-color:Green;color:Yellow;">为html控件绑定服务器控件的两个要点:<br />  
  15.     1.onserverclick="serverMethod"这里只写方法名.<br />  
  16.     2.后台代码,必须是<br />  
  17.     protected void XmlAdd(object sender, EventArgs e){}<br />  
  18.     注意两个参数及保护级.  
  19.     </div>  
  20.     <input id="btnAdd" type="button" value="add" runat="server" onserverclick="XmlAdd" />  
  21.     <input id="btnDelete" type="button" value="delete" runat="server" onserverclick="XmlDelete" />  
  22.     <input id="btnUpdate" type="button" value="update" runat="server" onserverclick="XmlUpdate" />  
  23.     <input id="btnQuery" type="button" value="query" runat="server" onserverclick="XmlQuery" />  
  24.     </form>  
  25. </body>  
  26. </html>  

后台:

  1. using System;  
  2. using System.Data;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5. using System.Web;  
  6. using System.Web.Security;  
  7. using System.Web.UI;  
  8. using System.Web.UI.WebControls;  
  9. using System.Web.UI.WebControls.WebParts;  
  10. using System.Web.UI.HtmlControls;  
  11.   
  12. using System.Xml;  
  13.   
  14. public partial class test_Default : System.Web.UI.Page  
  15. {  
  16.     string xmlFile = System.Configuration.ConfigurationManager.AppSettings["xmlFile"];  
  17.     XmlDocument XmlDoc = new XmlDocument();  
  18.     protected void Page_Load(object sender, EventArgs e)  
  19.     {  
  20.         Bind();  
  21.     }  
  22.     private void Bind()  
  23.     {  
  24.         XmlDoc.Load(Server.MapPath("../" + xmlFile));//向上一级  
  25.         this.showXml.InnerHtml = System.Web.HttpUtility.HtmlEncode(XmlDoc.InnerXml);  
  26.     }  
  27.     protected void XmlAdd(object sender, EventArgs e)  
  28.     {  
  29.         XmlNode objRootNode = XmlDoc.SelectSingleNode("//Root");    //声明XmlNode对象  
  30.         XmlElement objChildNode = XmlDoc.CreateElement("Student");  //创建XmlElement对象  
  31.         objChildNode.SetAttribute("id""1");  
  32.         objRootNode.AppendChild(objChildNode);  
  33.         //  
  34.         XmlElement objElement = XmlDoc.CreateElement("Name");//???结点和元素的区别?方法都一样.  
  35.         objElement.InnerText = "tree1";  
  36.         objChildNode.AppendChild(objElement);  
  37.         //保存  
  38.         XmlDoc.Save(Server.MapPath("../" + xmlFile));  
  39.     }  
  40.     protected void XmlDelete(object sender, EventArgs e)  
  41.     {  
  42.         string Node = "//Root/Student[Name='tree1']";//Xml是严格区分大小写的.  
  43.         XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));  
  44.         //保存  
  45.         XmlDoc.Save(Server.MapPath("../" + xmlFile));  
  46.     }  
  47.     protected void XmlUpdate(object sender, EventArgs e)  
  48.     {  
  49.         //XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']/Name").InnerText = "tree2";  
  50.         XmlDoc.SelectSingleNode("//Root/Student[Name='tree1']").Attributes["id"].Value = "001";  
  51.         //保存  
  52.         XmlDoc.Save(Server.MapPath("../" + xmlFile));  
  53.     }  
  54.     protected void XmlQuery(object sender, EventArgs e)  
  55.     {  
  56.         XmlNodeList NodeList = XmlDoc.SelectNodes("//Root/Student");//查询全部的student节点  
  57.         //循环遍历节点,查询是否存在该节点  
  58.         for (int i = 0; i < NodeList.Count; i++)  
  59.         {  
  60.             Response.Write(NodeList[i].ChildNodes[0].InnerText);  
  61.         }  
  62.   
  63.         //查询单个节点,//表示全部匹配的元素./表示以此为根的子元素.javascript下的查询也是一样.  
  64.         string XmlPathNode = "//Root/Student[Name='rock']/Photo";  
  65.         Response.Write(XmlDoc.SelectSingleNode(XmlPathNode).InnerText);  
  66.     }  
  67. }  

xml文件:

  1. <?xml version="1.0" encoding="gb2312"?>  
  2. <Root>  
  3.   <Student Admin="no">  
  4.     <Name>rock</Name>  
  5.     <NickName>rock1</NickName>  
  6.     <Pwd>123</Pwd>  
  7.     <Sex>男生</Sex>  
  8.     <Birthday>1986-1-1</Birthday>  
  9.     <Email>xymac@163.com</Email>  
  10.     <QQ>123374355</QQ>  
  11.     <Msn>loveplc@live.cn</Msn>  
  12.     <Tel>13005129336</Tel>  
  13.     <Homepage>http://www.loveplc.cn</Homepage>  
  14.     <Address>广州</Address>  
  15.     <Work>asp.net菜鸟</Work>  
  16.     <Photo>images/rock.gif</Photo>  
  17.     <Time>2008-3-18 10:15:29</Time>  
  18.   </Student>  
  19.   <Student Admin="yes">  
  20.     <Name>tree</Name>  
  21.     <NickName>宿舍老大</NickName>  
  22.     <Pwd>51aspx</Pwd>  
  23.     <Sex>男生</Sex>  
  24.     <Birthday>  
  25.     </Birthday>  
  26.     <Email>support@tree.com</Email>  
  27.     <QQ>  
  28.     </QQ>  
  29.     <Msn>  
  30.     </Msn>  
  31.     <Tel>  
  32.     </Tel>  
  33.     <Homepage>  
  34.     </Homepage>  
  35.     <Address>  
  36.     </Address>  
  37.     <Work>  
  38.     </Work>  
  39.     <Photo>  
  40.     </Photo>  
  41.     <Time>2008-3-26 11:39:57</Time>  
  42.   </Student>  
  43.   <Student>  
  44.     <Name>tree2</Name>  
  45.   </Student>  
  46.   <Student id="001">  
  47.     <Name>tree1</Name>  
  48.   </Student>  
  49. </Root> 

posted on 2010-07-13 00:05  jianshaohui  阅读(499)  评论(0编辑  收藏  举报

导航