XML2.aspx.cs
资源名称:XMLOp.rar [点击查看]
上传用户:feifan318
上传日期:2022-08-10
资源大小:5k
文件大小:3k
源码类别:
xml/soap/webservice
开发平台:
JavaScript
- using System;
- using System.Data;
- using System.Configuration;
- using System.Collections;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Web.UI.HtmlControls;
- using System.Xml;
- public partial class XML2 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- XmlTextWriter xmlwriter;
- xmlwriter = new XmlTextWriter(Server.MapPath("Employee.xml"), System.Text.Encoding.Default);
- xmlwriter.Formatting = Formatting.Indented;
- xmlwriter.WriteStartDocument();
- xmlwriter.WriteStartElement("Employees");
- xmlwriter.WriteStartElement("Employee");
- xmlwriter.WriteAttributeString("EmpID", "1");
- xmlwriter.WriteStartElement("EmpID");
- xmlwriter.WriteString("1");
- xmlwriter.WriteEndElement();
- xmlwriter.WriteStartElement("EmpName");
- xmlwriter.WriteString("lee");
- xmlwriter.WriteEndElement();
- xmlwriter.WriteStartElement("EmpSex");
- xmlwriter.WriteString("Male");
- xmlwriter.WriteEndElement();
- xmlwriter.WriteEndElement();
- xmlwriter.WriteEndDocument();
- xmlwriter.Flush();
- xmlwriter.Close();
- }
- protected void btnRead_Click(object sender, EventArgs e)
- {
- //XmlTextReader xmlreader = new XmlTextReader(Server.MapPath("Employee.xml"));
- //读XML
- this.TextBox1.Text = "";
- XmlDocument xmldoc = new XmlDocument();
- try
- {
- xmldoc.Load(Server.MapPath("StudentXML.xml"));
- }
- catch (Exception ex)
- {
- this.TextBox1.Text = "找不到XML,请先创建";
- }
- XmlNodeList nodeList = xmldoc.SelectSingleNode("Students").ChildNodes;
- this.TextBox1.Text = "<?xml version='1.0'?>n<Students>n";
- foreach (XmlNode xmlnode in nodeList)
- {
- XmlElement xe = (XmlElement)xmlnode;
- this.TextBox1.Text += " <" + xe.LocalName + " ID=" + xe.GetAttribute("ID") + ">n";
- XmlNodeList xmlnodelist = xmlnode.ChildNodes;
- foreach (XmlNode xmlChildNode in xmlnodelist)
- {
- XmlElement xechild = (XmlElement)xmlChildNode;
- switch (xechild.LocalName)
- {
- case "ID":
- this.TextBox1.Text += " <ID>" + xechild.InnerText + "</ID>n";
- break;
- case "Name":
- this.TextBox1.Text += " <Name>" + xechild.InnerText + "</Name>n";
- break;
- case "Sex":
- this.TextBox1.Text += " <Sex>" + xechild.InnerText + "</Sex>n";
- break;
- }
- }
- this.TextBox1.Text += " </Student>n";
- }
- this.TextBox1.Text += "</Students>";
- }
- }