Default.aspx.cs
资源名称:XMLOp.rar [点击查看]
上传用户:feifan318
上传日期:2022-08-10
资源大小:5k
文件大小:5k
源码类别:
xml/soap/webservice
开发平台:
JavaScript
- using System;
- using System.Data;
- using System.Configuration;
- 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 _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- { }
- }
- /// <summary>
- /// creating XML
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnCreate_Click(object sender, EventArgs e)
- {
- XmlDocument xmldoc;
- XmlNode xmlnode;
- XmlElement xmlelement;
- xmldoc = new XmlDocument();
- try
- {
- xmldoc.Load(Server.MapPath("StudentXML.xml"));
- this.TextBox1.Text = "XML已经创建,不能重复创建";
- }
- catch
- {
- xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); //插入<?xml version='1.0'?>
- xmldoc.AppendChild(xmlnode);//添加到xml中
- xmlelement = xmldoc.CreateElement(null, "Students", null); //创建元素---根元素Student
- xmldoc.AppendChild(xmlelement);
- //for (int i = 0; i < 10; i++)
- //{
- // XmlNode root = xmldoc.SelectSingleNode("Students");
- // XmlElement xmlstu = xmldoc.CreateElement("Student");
- // xmlstu.SetAttribute("ID", (i + 1).ToString());
- // XmlElement xmleleID = xmldoc.CreateElement("ID");
- // xmleleID.InnerText = (i + 1).ToString();
- // XmlElement xmleleName = xmldoc.CreateElement("Name");
- // xmleleName.InnerText = "lee" + (i + 1).ToString();
- // XmlElement xmlelesex = xmldoc.CreateElement("Sex");
- // if (i % 2 == 0)
- // {
- // xmlelesex.InnerText = "Male";
- // }
- // else
- // xmlelesex.InnerText = "Female";
- // xmlstu.AppendChild(xmleleID);
- // xmlstu.AppendChild(xmleleName);
- // xmlstu.AppendChild(xmlelesex);
- // root.AppendChild(xmlstu);
- //}
- xmldoc.Save(Server.MapPath("StudentXML.xml"));
- }
- }
- /// <summary>
- /// Reading XML
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void Button1_Click(object sender, EventArgs e)
- {
- try
- {
- ReadXML();
- }
- catch (Exception ex)
- {
- this.TextBox1.Text = ex.Message;
- }
- }
- public void ReadXML()
- {
- 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>";
- }
- /// <summary>
- /// Add XML Element
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- XmlDocument xmldoc = new XmlDocument();
- try
- {
- xmldoc.Load(Server.MapPath("StudentXML.xml"));
- }
- catch (Exception ex)
- {
- this.TextBox1.Text = "找不到XML,请先创建";
- }
- XmlNode node = xmldoc.SelectSingleNode("Students");
- XmlElement xmlstu = xmldoc.CreateElement("Student");
- xmlstu.SetAttribute("ID", this.txtID.Text);
- XmlElement xmleleID = xmldoc.CreateElement("ID");
- xmleleID.InnerText = this.txtID.Text;
- XmlElement xmleleName = xmldoc.CreateElement("Name");
- xmleleName.InnerText = this.txtName.Text;
- XmlElement xmlelesex = xmldoc.CreateElement("Sex");
- xmlelesex.InnerText = this.txtSex.Text;
- xmlstu.AppendChild(xmleleID);
- xmlstu.AppendChild(xmleleName);
- xmlstu.AppendChild(xmlelesex);
- node.AppendChild(xmlstu);
- xmldoc.Save(Server.MapPath("StudentXML.xml"));
- this.Button1_Click(sender, e);
- }
- }