|
用ASP.NET和XML做的新闻系统(3) <%@ Import Namespace="System"%> <HTML> <HEAD> <script language="C#" runat="server"> public void Button1_Click(object sender, System.EventArgs e) { //判断文件是否存在 if(File.Exists(Server.MapPath(TextBox1.Text +".xml"))) { Response.Write("文件名已经存在,请重选文件名。"); Response.End() ; } else { XmlNode currNode; XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Server.MapPath("contents.xml")); string InsStr="<topic><title>"+TextBox2.Text+"</title><href> main.aspx?name="+TextBox1.Text+"</href></topic>"; XmlDocumentFragment docFrag = xmldoc.CreateDocumentFragment(); docFrag.InnerXml = InsStr; currNode = xmldoc.DocumentElement; currNode.InsertAfter(docFrag, currNode.LastChild); //save the output to a file xmldoc.Save (Server.MapPath("contents.xml")); //把TextBox5中的文件换成符合xml格式的内容。 string xmlfile =TextBox5.Text.Replace("&","&"); xmlfile = xmlfile.Replace("<","<"); xmlfile = xmlfile.Replace(">",">"); xmlfile = xmlfile.Replace( @"""""","""); xmlfile = xmlfile.Replace(""","'"); xmlfile = xmlfile.Replace ("\n","</paragraph><paragraph>"); //把数据写入新建的xml文件中去。 XmlDocument doc = new XmlDocument(); doc.LoadXml ("<?xml version="1.0" encoding="GB2312"?> <document><title>"+TextBox2.Text +"</title><abstract>"+ TextBox4.Text "</abstract><author>"+TextBox3.Text+ "</author><content><paragraph>"+xmlfile+"</paragraph> </content></document>"); doc.Save (Server.MapPath(TextBox1.Text +".xml")); Response.Write("You hava input the article!"); TextBox1.Text=""; TextBox2.Text=""; TextBox3.Text=""; TextBox4.Text=""; TextBox5.Text=""; } //向目录文件中写数据 } public void Button2_Click(object sender, System.EventArgs e) {} </script> <meta content="Internet EXPlorer 5.0" name=vs_targetSchema>
|