xml学习之 XmlWriter

这个和输出流差不多额

其实个人认为 写这个太麻烦了 写着写着就头晕了。。。

前台:

放一个Button就好

 

后台

protected void Button1_Click(object sender, EventArgs e)
        {
            XmlWriterSettings set = new XmlWriterSettings();
            set.Encoding = Encoding.UTF8;
            set.Indent = true;
            using (XmlWriter writer = XmlWriter.Create(Server.MapPath("newbook.xml"), set))
            {
                writer.WriteStartDocument();

                writer.WriteStartElement("books");
                writer.WriteStartAttribute("id");
                writer.WriteValue("1");                                                   //偶主要头晕的就是这。。。。太多重复了额
                writer.WriteEndAttribute();
                writer.WriteStartElement("book");
                writer.WriteStartElement("author");
                writer.WriteString("张三");
                writer.WriteEndElement();
               
                writer.WriteEndElement();
               

                writer.WriteStartElement("book");               
                writer.WriteStartElement("author");
                writer.WriteString("李斯");
                writer.WriteEndElement();
               
                writer.WriteEndElement();
               

                writer.WriteStartElement("book");
                writer.WriteStartElement("author");
                writer.WriteString("王五");     
                writer.WriteEndElement();
               
                writer.WriteEndElement();             
               

                writer.WriteEndElement();               

            }

            Response.Write("<script>alert('创建完成啊啊啊啊啊啊啊啊啊')</script>");

        }

 

 

你可能感兴趣的:(xml学习之 XmlWriter)