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

xml/soap/webservice

开发平台:

JavaScript

  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. using System.Xml;
  12. public partial class XML2 : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.     }
  17.     protected void Button1_Click(object sender, EventArgs e)
  18.     {
  19.         XmlTextWriter xmlwriter;
  20.         xmlwriter = new XmlTextWriter(Server.MapPath("Employee.xml"), System.Text.Encoding.Default);
  21.         xmlwriter.Formatting = Formatting.Indented;
  22.         xmlwriter.WriteStartDocument();
  23.         xmlwriter.WriteStartElement("Employees");
  24.         xmlwriter.WriteStartElement("Employee");
  25.         xmlwriter.WriteAttributeString("EmpID", "1");
  26.         xmlwriter.WriteStartElement("EmpID");
  27.         xmlwriter.WriteString("1");
  28.         xmlwriter.WriteEndElement();
  29.         xmlwriter.WriteStartElement("EmpName");
  30.         xmlwriter.WriteString("lee");
  31.         xmlwriter.WriteEndElement();
  32.         xmlwriter.WriteStartElement("EmpSex");
  33.         xmlwriter.WriteString("Male");
  34.         xmlwriter.WriteEndElement();
  35.         xmlwriter.WriteEndElement();
  36.         xmlwriter.WriteEndDocument();
  37.         xmlwriter.Flush();
  38.         xmlwriter.Close();
  39.         
  40.     }
  41.     protected void btnRead_Click(object sender, EventArgs e)
  42.     {
  43.         //XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Employee.xml"));
  44.         //读XML
  45.         this.TextBox1.Text = "";
  46.         XmlDocument xmldoc = new XmlDocument();
  47.         try
  48.         {
  49.             xmldoc.Load(Server.MapPath("StudentXML.xml"));
  50.         }
  51.         catch (Exception ex)
  52.         {
  53.             this.TextBox1.Text = "找不到XML,请先创建";
  54.         }
  55.         XmlNodeList nodeList = xmldoc.SelectSingleNode("Students").ChildNodes;
  56.         this.TextBox1.Text = "<?xml version='1.0'?>n<Students>n";
  57.         foreach (XmlNode xmlnode in nodeList)
  58.         {
  59.             XmlElement xe = (XmlElement)xmlnode;
  60.             this.TextBox1.Text += "    <" + xe.LocalName + " ID=" + xe.GetAttribute("ID") + ">n";
  61.             XmlNodeList xmlnodelist = xmlnode.ChildNodes;
  62.             foreach (XmlNode xmlChildNode in xmlnodelist)
  63.             {
  64.                 XmlElement xechild = (XmlElement)xmlChildNode;
  65.                 switch (xechild.LocalName)
  66.                 {
  67.                     case "ID":
  68.                         this.TextBox1.Text += "        <ID>" + xechild.InnerText + "</ID>n";
  69.                         break;
  70.                     case "Name":
  71.                         this.TextBox1.Text += "        <Name>" + xechild.InnerText + "</Name>n";
  72.                         break;
  73.                     case "Sex":
  74.                         this.TextBox1.Text += "        <Sex>" + xechild.InnerText + "</Sex>n";
  75.                         break;
  76.                 }
  77.             }
  78.             this.TextBox1.Text += "    </Student>n";
  79.         }
  80.         this.TextBox1.Text += "</Students>";
  81.     }
  82. }