Default.aspx.cs
上传用户:feifan318
上传日期:2022-08-10
资源大小:5k
文件大小:5k
源码类别:

xml/soap/webservice

开发平台:

JavaScript

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Xml;
  11. public partial class _Default : System.Web.UI.Page 
  12. {
  13.     protected void Page_Load(object sender, EventArgs e)
  14.     {
  15.         if (!Page.IsPostBack)
  16.         { }
  17.     }
  18.     /// <summary>
  19.     /// creating XML 
  20.     /// </summary>
  21.     /// <param name="sender"></param>
  22.     /// <param name="e"></param>
  23.     protected void btnCreate_Click(object sender, EventArgs e)
  24.     {
  25.         XmlDocument xmldoc;
  26.         XmlNode xmlnode;
  27.         XmlElement xmlelement;
  28.         xmldoc = new XmlDocument();
  29.         try
  30.         {
  31.             xmldoc.Load(Server.MapPath("StudentXML.xml"));
  32.             this.TextBox1.Text = "XML已经创建,不能重复创建";
  33.         }
  34.         catch
  35.         {
  36.             xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); //插入<?xml version='1.0'?>
  37.             xmldoc.AppendChild(xmlnode);//添加到xml中
  38.             xmlelement = xmldoc.CreateElement(null, "Students", null); //创建元素---根元素Student
  39.             xmldoc.AppendChild(xmlelement);
  40.             //for (int i = 0; i < 10; i++)
  41.             //{
  42.             //    XmlNode root = xmldoc.SelectSingleNode("Students");
  43.             //    XmlElement xmlstu = xmldoc.CreateElement("Student");
  44.             //    xmlstu.SetAttribute("ID", (i + 1).ToString());
  45.             //    XmlElement xmleleID = xmldoc.CreateElement("ID");
  46.             //    xmleleID.InnerText = (i + 1).ToString();
  47.             //    XmlElement xmleleName = xmldoc.CreateElement("Name");
  48.             //    xmleleName.InnerText = "lee" + (i + 1).ToString();
  49.             //    XmlElement xmlelesex = xmldoc.CreateElement("Sex");
  50.             //    if (i % 2 == 0)
  51.             //    {
  52.             //        xmlelesex.InnerText = "Male";
  53.             //    }
  54.             //    else
  55.             //        xmlelesex.InnerText = "Female";
  56.             //    xmlstu.AppendChild(xmleleID);
  57.             //    xmlstu.AppendChild(xmleleName);
  58.             //    xmlstu.AppendChild(xmlelesex);
  59.             //    root.AppendChild(xmlstu);
  60.             //}
  61.             xmldoc.Save(Server.MapPath("StudentXML.xml"));
  62.         }
  63.     }
  64.     /// <summary>
  65.     /// Reading XML
  66.     /// </summary>
  67.     /// <param name="sender"></param>
  68.     /// <param name="e"></param>
  69.     protected void Button1_Click(object sender, EventArgs e)
  70.     {
  71.         try
  72.         {
  73.             ReadXML();
  74.         }
  75.         catch (Exception ex)
  76.         {
  77.             this.TextBox1.Text = ex.Message;
  78.         }
  79.         
  80.     }
  81.     public void ReadXML()
  82.     {
  83.         this.TextBox1.Text = "";
  84.         XmlDocument xmldoc = new XmlDocument();
  85.         try
  86.         {
  87.             xmldoc.Load(Server.MapPath("StudentXML.xml"));
  88.         }
  89.         catch (Exception ex)
  90.         {
  91.             this.TextBox1.Text = "找不到XML,请先创建";
  92.         }
  93.         XmlNodeList nodeList = xmldoc.SelectSingleNode("Students").ChildNodes;
  94.         this.TextBox1.Text = "<?xml version='1.0'?>n<Students>n";
  95.         foreach (XmlNode xmlnode in nodeList)
  96.         {
  97.             XmlElement xe = (XmlElement)xmlnode;
  98.             this.TextBox1.Text += "    <" + xe.LocalName + " ID=" + xe.GetAttribute("ID") + ">n";
  99.             XmlNodeList xmlnodelist = xmlnode.ChildNodes;
  100.             foreach (XmlNode xmlChildNode in xmlnodelist)
  101.             {
  102.                 XmlElement xechild = (XmlElement)xmlChildNode;
  103.                 switch (xechild.LocalName)
  104.                 {
  105.                     case "ID":
  106.                         this.TextBox1.Text += "        <ID>" + xechild.InnerText + "</ID>n";
  107.                         break;
  108.                     case "Name":
  109.                         this.TextBox1.Text += "        <Name>" + xechild.InnerText + "</Name>n";
  110.                         break;
  111.                     case "Sex":
  112.                         this.TextBox1.Text += "        <Sex>" + xechild.InnerText + "</Sex>n";
  113.                         break;
  114.                 }
  115.             }
  116.             this.TextBox1.Text += "    </Student>n";
  117.         }
  118.         this.TextBox1.Text += "</Students>";
  119.     }
  120.     /// <summary>
  121.     /// Add XML Element
  122.     /// </summary>
  123.     /// <param name="sender"></param>
  124.     /// <param name="e"></param>
  125.     protected void btnAdd_Click(object sender, EventArgs e)
  126.     {
  127.         XmlDocument xmldoc = new XmlDocument();
  128.         try
  129.         {
  130.             xmldoc.Load(Server.MapPath("StudentXML.xml"));
  131.         }
  132.         catch (Exception ex)
  133.         {
  134.             this.TextBox1.Text = "找不到XML,请先创建";
  135.         }
  136.         XmlNode node = xmldoc.SelectSingleNode("Students");
  137.         XmlElement xmlstu = xmldoc.CreateElement("Student");
  138.         xmlstu.SetAttribute("ID", this.txtID.Text);
  139.         XmlElement xmleleID = xmldoc.CreateElement("ID");
  140.         xmleleID.InnerText = this.txtID.Text;
  141.         XmlElement xmleleName = xmldoc.CreateElement("Name");
  142.         xmleleName.InnerText = this.txtName.Text;
  143.         XmlElement xmlelesex = xmldoc.CreateElement("Sex");
  144.         xmlelesex.InnerText = this.txtSex.Text;
  145.         xmlstu.AppendChild(xmleleID);
  146.         xmlstu.AppendChild(xmleleName);
  147.         xmlstu.AppendChild(xmlelesex);
  148.         node.AppendChild(xmlstu);
  149.         xmldoc.Save(Server.MapPath("StudentXML.xml"));
  150.         this.Button1_Click(sender, e);
  151.     }
  152. }