在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
.aspx文件
View Code
.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 } } |
请发表评论