c#操作xml练习.
.aspx文件
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XmlTest.aspx.cs" Inherits="XmlTest" validateRequest="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>XML操作练习</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="LinkButtonCreate" runat="server" OnClick="LinkButtonCreate_Click">创建Xml文件</asp:LinkButton>
<asp:LinkButton ID="LinkButtonAdd" runat="server" OnClick="LinkButtonAdd_Click">批量添加Book结点</asp:LinkButton>
<asp:LinkButton ID="LinkButtonShow" runat="server" OnClick="LinkButtonShow_Click">显示Xml内容</asp:LinkButton>
<asp:LinkButton ID="LinkButtonDel" runat="server" OnClick="LinkButtonDel_Click">删除全部结点</asp:LinkButton>
<hr />
子结点名:<asp:TextBox ID="TextBoxNode" runat="server">author</asp:TextBox>
子结点值:<asp:TextBox ID="TextBoxNodeValue" runat="server">张三</asp:TextBox>
<asp:LinkButton ID="LinkButtonSearchNode" runat="server" onclick="LinkButtonSearchNode_Click">查找结点</asp:LinkButton>
修改子结点值:<asp:TextBox ID="TextBoxEditNodeValue" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButtonEdit" runat="server" OnClick="LinkButtonSearchNode_Click">修改结点</asp:LinkButton>
<asp:LinkButton ID="LinkButtonDelNode" runat="server" OnClick="LinkButtonDelNode_Click">删除结点(可以删除多个结点)</asp:LinkButton>
<hr />
book属性Id值:<asp:TextBox ID="TextBoxAttribute" runat="server">2</asp:TextBox>
子结点:<asp:TextBox ID="TextBoxChildNode" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButtonSearch" runat="server" onclick="LinkButtonSearch_Click">查找</asp:LinkButton>
<br />
属性名称:<asp:TextBox ID="TextBoxAttributeName" runat="server"></asp:TextBox>
属性值:<asp:TextBox ID="TextBoxAttributeValue" runat="server"></asp:TextBox>
<asp:LinkButton ID="LinkButtonAdd2" runat="server" OnClick="LinkButtonAdd2_Click">添加修改属性</asp:LinkButton>
<asp:LinkButton ID="LinkButtonDelAttribute" runat="server" OnClick="LinkButtonDelAttribute_Click">删除属性</asp:LinkButton>
<hr />
<asp:Label ID="Label1" runat="server" Text="这里显示提示信息!" ForeColor="#FF3300"></asp:Label>
<hr />
<asp:TextBox ID="TextBox1" runat="server" Height="330px" Width="617px" TextMode="MultiLine"></asp:TextBox>
<iframe id="IframeTT" runat="server" src="xml/booktest.xml" height="330px" width="617px" frameborder="1" style="border-style: solid; border-width: 0px" ></iframe>
</div>
</form>
</body>
</html>
.cs文件
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;
using System.Collections.Generic;
public partial class XmlTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButtonCreate_Click(object sender, EventArgs e)
{
string path = Server.MapPath("Xml/bookTest.xml");
XmlDocument Doc = new XmlDocument();
XmlDeclaration DecA = Doc.CreateXmlDeclaration("1.0", null, null); //默认为utf-8
Doc.AppendChild(DecA);
//创建一个根节点(一级)
XmlElement Root = Doc.CreateElement("Root");
Doc.AppendChild(Root);
//创建节点(二级) XmlNode不能直接带属性
XmlNode node2 = Doc.CreateElement("Book");
Root.AppendChild(node2);
//创建节点(二级)只带属性 ----注意XmlElement和XmlNode不同,XmlElement可以有属性
XmlElement Element = Doc.CreateElement("Book");
Element.SetAttribute("Id", "1");
Element.SetAttribute("Name", "test111");
Element.SetAttribute("PageCount", "111");
Root.AppendChild(Element);
//创建节点(二级)带属性和子节点 ----注意XmlElement和XmlNode不同,XmlElement可以有属性
XmlElement Element1 = Doc.CreateElement("Book");
Element1.SetAttribute("Id", "2");
Element1.SetAttribute("Name", "test222");
Element1.SetAttribute("PageCount", "222");
Root.AppendChild(Element1);
//创建节点(三级)只有值
XmlElement Element2 = Doc.CreateElement("price");
Element2.InnerText = "22.50";
Element1.AppendChild(Element2);
//创建节点(三级)只有值
Element2 = Doc.CreateElement("author");
Element2.InnerText = "张三";
Element1.AppendChild(Element2);
//创建节点(三级)只有值
Element2 = Doc.CreateElement("Content");
Element2.InnerText = "简介Test简介Test简介Test简介Test简介Test简介Test简介Test简介Test简介Test简介Test简介Test简介Test";
Element1.AppendChild(Element2);
Doc.Save(@path);
Label1.Text=DateTime.Now.ToString()+"创建XML文件:"+path+"成功!";
TextBox1.Text = System.IO.File.ReadAllText(@path);
}
protected void LinkButtonAdd_Click(object sender, EventArgs e)
{
string path;
path = Server.MapPath("xml/bookTest.xml");
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
XmlNode Root = Doc.SelectSingleNode("Root");
int NodeCount=Root.ChildNodes.Count;//结点个数
for (int i = NodeCount; i < 10 + NodeCount; i++)
{
//创建新节点(只有属性)
XmlElement Element1 = Doc.CreateElement("Book");
Element1.SetAttribute("Id", i.ToString());
Element1.SetAttribute("Name", "book"+i.ToString());
Element1.SetAttribute("PageCount", "100"+i.ToString());
//创建子节点(只有值)
XmlElement Element2 = Doc.CreateElement("price");
Element2.InnerText = "10" + i.ToString() + ".50";
Element1.AppendChild(Element2);
//创建子节点(只有值)
Element2 = Doc.CreateElement("author");
Element2.InnerText = "张三("+i.ToString()+")";
Element1.AppendChild(Element2);
//创建子节点(只有值)
Element2 = Doc.CreateElement("Content");
Element2.InnerText = "简介("+i.ToString()+")Test简介Test简介Test";
Element1.AppendChild(Element2);
Root.AppendChild(Element1);
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "显示XML文件:" + path + "成功!";//提示
TextBox1.Text = System.IO.File.ReadAllText(@path);//输出到TextBox
}
protected void LinkButtonDel_Click(object sender, EventArgs e)
{
string path;
path = Server.MapPath("xml/bookTest.xml");
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
XmlNode Root = Doc.SelectSingleNode("Root");
Root.RemoveAll();
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "清除XML文件:" + path + "成功!";//提示
TextBox1.Text = System.IO.File.ReadAllText(@path);//输出到TextBox
}
protected void LinkButtonShow_Click(object sender, EventArgs e)
{
string XmlContent,path;
path = Server.MapPath("xml/bookTest.xml");
XmlContent = System.IO.File.ReadAllText(@path);
TextBox1.Text = XmlContent;
Label1.Text = DateTime.Now.ToString() + "显示XML文件:" + path + "成功!";
IframeTT.ResolveUrl("xml/booktest.xml");
}
protected void LinkButtonAdd2_Click(object sender, EventArgs e)
{
string SearchAttribute = TextBoxAttribute.Text;
string SearchTextBoxChildNode = TextBoxChildNode.Text;
string AddAttributeName = TextBoxAttributeName.Text;
string AddAttributeValue = TextBoxAttributeValue.Text;
string path = Server.MapPath("xml/bookTest.xml");
string ShowStr = "";
if (SearchTextBoxChildNode!="")
{
SearchTextBoxChildNode = "/" + SearchTextBoxChildNode;
}
if (SearchAttribute == "" && AddAttributeName == "" )
{
Common.JScript.Alert("属性ID 与 添加属性名称 必填");
}
else
{
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
XmlElement Node =(XmlElement) Doc.SelectSingleNode("Root/Book[@Id='" + SearchAttribute + "']" + SearchTextBoxChildNode);
if (Node != null)
{
Node.SetAttribute(AddAttributeName, AddAttributeValue);
if (SearchTextBoxChildNode != "")
{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
ShowStr = ShowStr + "\n-----结点值:" + Node.InnerText;
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
}
else
{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
//添加属性
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
foreach (XmlElement item in Node.ChildNodes)
{
ShowStr = ShowStr + "\n子结点:" + item.Name + "=" + item.InnerText;
}
}
}
else
{
ShowStr = "没有找到!";
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "查找结点:" + path + "成功!";//提示
TextBox1.Text = ShowStr;//输出到TextBox
}
}
protected void LinkButtonDelAttribute_Click(object sender, EventArgs e)
{
string SearchAttribute = TextBoxAttribute.Text;
string SearchTextBoxChildNode = TextBoxChildNode.Text;
string AddAttributeName = TextBoxAttributeName.Text;
string AddAttributeValue = TextBoxAttributeValue.Text;
string path = Server.MapPath("xml/bookTest.xml");
string ShowStr = "";
if (SearchTextBoxChildNode != "")
{
SearchTextBoxChildNode = "/" + SearchTextBoxChildNode;
}
if (SearchAttribute == "" && AddAttributeName == "")
{
Common.JScript.Alert("属性ID 与 添加属性名称 必填");
}
else
{
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
XmlElement Node = (XmlElement)Doc.SelectSingleNode("Root/Book[@Id='" + SearchAttribute + "']" + SearchTextBoxChildNode);
if (Node != null)
{
//删除结点属性
Node.RemoveAttribute(AddAttributeName);
if (SearchTextBoxChildNode != "")
{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
ShowStr = ShowStr + "\n-----结点值:" + Node.InnerText;
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
}
else
{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
//添加属性
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
foreach (XmlElement item in Node.ChildNodes)
{
ShowStr = ShowStr + "\n子结点:" + item.Name + "=" + item.InnerText;
}
}
}
else
{
ShowStr = "没有找到!";
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "删除结点属性:" + path + "成功!";//提示
TextBox1.Text = ShowStr;//输出到TextBox
}
}
/// <summary>
/// 查找
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButtonSearch_Click(object sender, EventArgs e)
{
string SearchNode=TextBoxNode.Text;
string SearchAttribute = TextBoxAttribute.Text;
string SearchTextBoxChildNode = TextBoxChildNode.Text;
string path = Server.MapPath("xml/bookTest.xml");
string ShowStr = "";
if (SearchTextBoxChildNode!="")
{
SearchTextBoxChildNode = "/" + SearchTextBoxChildNode;
}
if (SearchAttribute == "")
{
Common.JScript.Alert("属性ID必填");
}
else {
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
XmlNode Node = Doc.SelectSingleNode("Root/Book[@Id='" + SearchAttribute + "']" + SearchTextBoxChildNode);
if (Node != null)
{
if (SearchTextBoxChildNode != "")
{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
ShowStr = ShowStr + "\n-----结点值:" + Node.InnerText;
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
}else{
ShowStr = ShowStr + "\n-----结点:" + Node.Name;
foreach (XmlAttribute item in Node.Attributes)
{
ShowStr = ShowStr + "\n属性" + item.Name + "=" + item.Value;
}
foreach (XmlElement item in Node.ChildNodes)
{
ShowStr = ShowStr + "\n子结点:" + item.Name + "=" + item.InnerText;
}
}
}
else {
ShowStr = "没有找到!";
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "查找结点:" + path + "成功!";//提示
TextBox1.Text = ShowStr;//输出到TextBox
}
}
protected void LinkButtonSearchNode_Click(object sender, EventArgs e)
{
string SearchNode=TextBoxNode.Text;
string SearchNodeValue = TextBoxNodeValue.Text;
string SearchEditNodeValue = TextBoxEditNodeValue.Text;
string path = Server.MapPath("xml/bookTest.xml");
string ShowStr = "";
string ShowStr2 = "";
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
if (SearchNode == "")
{
Common.JScript.Alert("经查找的结点必须输入!");
}
else
{
XmlNodeList NodeList = Doc.GetElementsByTagName(SearchNode);
if (NodeList != null)
{
foreach (XmlNode item in NodeList)
{
if (!(SearchNodeValue.Equals("")))
{
if (SearchNodeValue.Equals(item.InnerText))
{
ShowStr = ShowStr + "\n结点值:" + item.InnerText;
//修改结点值
if (!(SearchEditNodeValue.Equals(""))) { item.InnerText = SearchEditNodeValue; }
ShowStr = ShowStr + "----父结点<" + item.ParentNode.Name;
foreach (XmlAttribute itemAttrib in item.ParentNode.Attributes)
{
ShowStr = ShowStr + " " + itemAttrib.Name + "=\"" + itemAttrib.Value + "\"";
}
ShowStr = ShowStr + " />";
}
else
{
ShowStr2 = " 没找到这个结点!";
}
}
else if (SearchNodeValue.Equals("") && SearchEditNodeValue.Equals(""))
{
ShowStr = ShowStr + "\n结点值:" + item.InnerText;
//修改结点值
if (!(SearchEditNodeValue.Equals(""))) { item.InnerText = SearchEditNodeValue; }
ShowStr = ShowStr + "----父结点<" + item.ParentNode.Name;
foreach (XmlAttribute itemAttrib in item.ParentNode.Attributes)
{
ShowStr = ShowStr + " " + itemAttrib.Name + "=\"" + itemAttrib.Value + "\"";
}
ShowStr = ShowStr + " />";
}
else
{
ShowStr2 = " 没找到这个结点!";
}
}
}
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "查找结点:" + path + "成功!";//提示
if (ShowStr.Equals(""))
{
TextBox1.Text = ShowStr2;//输出到TextBox
}
else
{
TextBox1.Text = ShowStr;//输出到TextBox
}
}
protected void LinkButtonDelNode_Click(object sender, EventArgs e)
{
string SearchNode = TextBoxNode.Text;
string SearchNodeValue = TextBoxNodeValue.Text;
string SearchEditNodeValue = TextBoxEditNodeValue.Text;
string path = Server.MapPath("xml/bookTest.xml");
string ShowStr = "";
string ShowStr2 = "";
XmlDocument Doc = new XmlDocument();
Doc.Load(@path);
if (SearchNode == "")
{
Common.JScript.Alert("经查找的结点必须输入!");
}
else
{
XmlNodeList NodeList = Doc.GetElementsByTagName(SearchNode);
XmlNode DelNodel=null;
List<XmlNode> ParentNode=new List<XmlNode>();
if (NodeList != null)
{
foreach (XmlNode item in NodeList)
{
if (SearchNodeValue != "" && item.InnerText == SearchNodeValue)
{
//得到删除结点
DelNodel = item;
}
else if (SearchNodeValue.Equals(""))
{
//删除结点
//item.ParentNode.RemoveChild(item);
ParentNode.Add(item.ParentNode);
}
else
{
ShowStr2 = "\n 没找到这个结点2!" ;
}
ShowStr = ShowStr +"\n"+ item.InnerText;
//Response.Write("[" + SearchNodeValue + "][" + item.InnerText + "]");
//if (item.InnerText == SearchNodeValue) { Common.JScript.Alert("LL"); }
}
if (DelNodel != null) { DelNodel.ParentNode.RemoveChild(DelNodel); }
if (ParentNode != null) {
foreach (XmlNode item in ParentNode)
{
item.RemoveChild( item.SelectSingleNode(SearchNode));
}
}
}
}
Doc.Save(@path);//保存
Label1.Text = DateTime.Now.ToString() + "查找结点:" + path + "成功!" + ShowStr2;//提示
TextBox1.Text = System.IO.File.ReadAllText(@path);//输出到TextBox
}
}
欢迎加入JAVA技术交流QQ群:179945282
欢迎加入ASP.NET(C#)交流QQ群:17534377


浙公网安备 33010602011771号