刚刚接触到XML,并不是很理解它的具体作用。
但是知道它可以存储数据,并在数据前台解析。
HTML里的文本应该可以写在XML内,方便修改。
下面是一种简单的显示XML的代码
<!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> <title> </title> </head> <body> <h1></h1> <p> <b>To:</b> <span id="uid"></span><br /> <b>From:</b> <span id="pwd"></span><br /> <b>Message:</b> <span id="age"></span> </p> <script type="text/javascript"> if(window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); } else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET","regist.xml",false); xmlhttp.send(); xmlDoc=xmlhttp.responseXML; document.getElementById("uid").innerHTML=xmlDoc.getElementsByTagName("uid")[0].childNodes[0].nodeValue; document.getElementById("pwd").innerHTML=xmlDoc.getElementsByTagName("pwd")[0].childNodes[0].nodeValue; document.getElementById("age").innerHTML=xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue; </script> </body> </html>
在open内写入自己的xml地址就可以用下面的操作显示出来了。
也可以用表格显示MLE中的内容
<!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"> <script type="text/javascript"> if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.open("GET", "RegisterInfo.xml", false); xmlhttp.send(); xmlDoc = xmlhttp.responseXML; x = xmlDoc.getElementsByTagName("Person"); function displayCDInfo(i) { artist = (x[i].getElementsByTagName("uid")[0].childNodes[0].nodeValue); title = (x[i].getElementsByTagName("pwd")[0].childNodes[0].nodeValue); year = (x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue); country = (x[i].getElementsByTagName("email")[0].childNodes[0].nodeValue); txt = "账号: " + artist + "<br />密码: " + title + "<br />年龄: " + year + "<br />邮箱: " + country; document.getElementById("showCD").innerHTML = txt; } </script> </head> <form> <body> <div id='showCD'>点击账号就可查询信息:</div><br /> <script type="text/javascript"> document.write("<table border='1'>"); document.write("<tr><td>"); document.write("用户账号"); document.write("</td></tr>"); for (var i = 0; i < x.length; i++) { document.write("<tr onclick='displayCDInfo(" + i + ")'>"); document.write("<td>"); document.write(x[i].getElementsByTagName("uid")[0].childNodes[0].nodeValue); document.write("</td></tr>"); } document.write("</table>"); </script> </form> </body> </html>
用xmlDocument 类来操作 XML
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.Text; public partial class _2008_Default2 : System.Web.UI.Page { XmlDocument xmldoc; XmlNode xmlnode; XmlElement xmlelem; protected void Page_Load(object sender, EventArgs e) { } private void makeXML() { xmldoc = new XmlDocument(); //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?> XmlDeclaration xmldecl; xmldecl = xmldoc.CreateXmlDeclaration("1.0", "gb2312", null); xmldoc.AppendChild(xmldecl); //加入一个根元素 xmlelem = xmldoc.CreateElement("", "Employees", ""); xmldoc.AppendChild(xmlelem); //加入另外一个元素 for (int i = 0; i < 2; i++) { XmlNode root = xmldoc.SelectSingleNode("Employees");//查找<Employees> XmlElement xe1 = xmldoc.CreateElement("Node");//创建一个<Node>节点 xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性 xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性 XmlElement xesub1 = xmldoc.CreateElement("title"); xesub1.InnerText = "CS从入门到精通";//设置文本节点 xe1.AppendChild(xesub1);//添加到<Node>节点中 XmlElement xesub2 = xmldoc.CreateElement("author"); xesub2.InnerText = "候捷"; xe1.AppendChild(xesub2); XmlElement xesub3 = xmldoc.CreateElement("price"); xesub3.InnerText = "58.3"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中 } //保存创建好的XML文档 xmldoc.Save(Server.MapPath("data.xml")); } private void makeXMl2() { XmlTextWriter xmlWriter; string strFilename = Server.MapPath("data1.xml"); xmlWriter = new XmlTextWriter(strFilename, Encoding.Default);//创建一个xml文档 xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("Employees"); xmlWriter.WriteStartElement("Node"); xmlWriter.WriteAttributeString("genre", "李赞红"); xmlWriter.WriteAttributeString("ISBN", "2-3631-4"); xmlWriter.WriteStartElement("title"); xmlWriter.WriteString("CS从入门到精通"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("author"); xmlWriter.WriteString("候捷"); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("price"); xmlWriter.WriteString("58.3"); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Close(); } protected void Button1_Click(object sender, EventArgs e) { makeXML(); makeXMl2(); } private void addxmldata() { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("data.xml")); XmlNode root = xmlDoc.SelectSingleNode("Employees");//查找<Employees> XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点 xe1.SetAttribute("genre", "张三");//设置该节点genre属性 xe1.SetAttribute("ISBN", "1-1111-1");//设置该节点ISBN属性 XmlElement xesub1 = xmlDoc.CreateElement("title"); xesub1.InnerText = "C#入门帮助";//设置文本节点 xe1.AppendChild(xesub1);//添加到<Node>节点中 XmlElement xesub2 = xmlDoc.CreateElement("author"); xesub2.InnerText = "高手"; xe1.AppendChild(xesub2); XmlElement xesub3 = xmlDoc.CreateElement("price"); xesub3.InnerText = "158.3"; xe1.AppendChild(xesub3); root.AppendChild(xe1);//添加到<Employees>节点中 xmlDoc.Save(Server.MapPath("data.xml")); } protected void Button2_Click(object sender, EventArgs e) { addxmldata(); } protected void Button3_Click(object sender, EventArgs e) { deleteXML(); } private void deleteXML()//删除结点 { XmlDocument xmlDoc=new XmlDocument(); xmlDoc.Load( Server.MapPath("data.xml") ); XmlNode root=xmlDoc.SelectSingleNode("Employees"); XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes; for (int i = 0; i < xnl.Count; i++) { XmlElement xe = (XmlElement)xnl.Item(i); if (xe.GetAttribute("genre") == "张三") { root.RemoveChild(xe); if (i < xnl.Count) i = i - 1; } } xmlDoc.Save(Server.MapPath("data.xml")); } private void updateXML()//修改结点的值(属性和子结点) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("data.xml")); XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute("genre") == "张三")//如果genre属性值为“张三” { xe.SetAttribute("genre", "update张三");//则修改该属性为“update张三” XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历 { XmlElement xe2 = (XmlElement)xn1;//转换类型 if (xe2.Name == "author")//如果找到 { xe2.InnerText = "亚胜";//则修改 } } } } xmlDoc.Save(Server.MapPath("data.xml"));//保存。 } protected void Button4_Click(object sender, EventArgs e) { updateXML(); } protected void Button5_Click(object sender, EventArgs e) { updateXML2(); } private void updateXML2()//修改结点(添加结点的属性和添加结点的自结点 { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("data.xml")); XmlNodeList nodeList = xmlDoc.SelectSingleNode("Employees").ChildNodes;//获取Employees节点的所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; xe.SetAttribute("test", "111111"); XmlElement xesub = xmlDoc.CreateElement("flag"); xesub.InnerText = "1"; xe.AppendChild(xesub); } xmlDoc.Save(Server.MapPath("data.xml")); } protected void Button6_Click(object sender, EventArgs e) { RemoveChild(); } private void RemoveChild()//删除结点中的某一个属性 { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(Server.MapPath("data.xml")); XmlNodeList xnl = xmlDoc.SelectSingleNode("Employees").ChildNodes; foreach (XmlNode xn in xnl) { XmlElement xe = (XmlElement)xn; xe.RemoveAttribute("genre");//删除genre属性 XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in nls)//遍历 { XmlElement xe2 = (XmlElement)xn1;//转换类型 if (xe2.Name == "flag")//如果找到 { xe.RemoveChild(xe2);//则删除 } } } xmlDoc.Save(Server.MapPath("data.xml")); } private void readXMl()//按照文本文件读取xml { System.IO.StreamReader myFile = new System.IO.StreamReader(Server.MapPath("data.xml"), System.Text.Encoding.Default); //注意System.Text.Encoding.Default string myString = myFile.ReadToEnd();//myString是读出的字符串 myFile.Close(); Response.Write(myString); } protected void Button7_Click(object sender, EventArgs e) { readXMl(); } }