版权声明:转载原创文章请以超链接形式请注明原文章出处,尊重作者,尊重原创!
恰饭广告
实现效果:
注意:页面分别拖拽两个Button和一个Label控件
WebForm1.aspx.cs主要代码:
protected void btnWrite_Click(object sender, EventArgs e) { string path = Server.MapPath("test.xml"); XmlTextWriter xml = new XmlTextWriter(path, System.Text.Encoding.UTF8); //xml文件的头部信息 xml.WriteStartDocument(); //自定义标签 xml.WriteStartElement("stuInfo"); xml.WriteStartElement("student"); xml.WriteElementString("stuName", "彬菌"); xml.WriteElementString("stuUrl", "www.idaobin.com"); //student标签的关闭标签 xml.WriteEndElement(); xml.WriteStartElement("student"); xml.WriteElementString("stuName", "彬酱"); xml.WriteElementString("stuUrl", "www.idaobin.com/life"); //student标签的关闭标签 xml.WriteEndElement(); //stuInfo标签的关闭标签 xml.WriteEndElement(); xml.WriteEndDocument(); xml.Flush(); xml.Close(); } protected void btnReader_Click(object sender, EventArgs e) { string path = Server.MapPath("test.xml"); XmlTextReader xml = new XmlTextReader(path); int index = 1; while (xml.Read()) { if (xml.NodeType == XmlNodeType.Element) { if (xml.Name == "student") { this.Label1.Text += "站点 " + index + " 信息如下:<hr />"; index++; } if (xml.Name == "stuName") { this.Label1.Text += "站点名称:" + xml.ReadElementString() + "<hr />"; } if (xml.Name == "stuUrl") { this.Label1.Text += "站点链接:" + xml.ReadElementString() + "<hr />"; } } } }
原文链接:https://www.idaobin.com/archives/1242.html
让我恰个饭吧.ヘ( ̄ω ̄ヘ)
恰饭广告