XML --- 使用XlmReader读books.xml中有几本书。使用XlmWriter写NewBooks.xml文件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace XML1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //使用XlmReader读books.xml中有几本书
            XmlReaderSettings settings=new XmlReaderSettings();
            settings.IgnoreComments=true;
            settings.IgnoreWhitespace=true;
            int booknum = 0;
            using (XmlReader reader = XmlReader.Create(Server.MapPath("books.xml"), settings))
            {
                    while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.LocalName == "book")
                        {
                            booknum++;
                        }
                    }
               
                }
 
            }
            Response.Write("一共找到了" + booknum + "本书!");
          
        }
        //使用XlmWriter写NewBooks.xml文件。

        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlWriterSettings settings=new XmlWriterSettings();
            settings.Encoding=System.Text.Encoding.UTF8;//输入的编码
            settings.Indent=true;
            using (XmlWriter write = XmlWriter.Create(Server.MapPath("newbook.xml"), settings))
            {
                write.WriteStartDocument();//开始写
                write.WriteStartElement("books");
                write.WriteStartElement("book");//book节点开始
                write.WriteStartAttribute("id");//给book添加属性
                write.WriteValue("1");//设置属性id的值为1
                write.WriteEndAttribute();//结束属性
                write.WriteStartElement("author");//author节点

                write.WriteString("张闻正");//author节点中的内容
                write.WriteEndElement();//结束author节点
                write.WriteEndElement();//结束book节点

                //再添一本书
                write.WriteStartElement("book");//book开始
                write.WriteStartElement("author");//author节点
                write.WriteString("清华大学出版社");//author节点中的内容
                write.WriteEndElement();//结束author节点
                write.WriteEndElement();//结束book节点

                write.WriteEndElement();
            }
            this.Response.Write("ok");

        }
    }
}

使用XlmWrite把一个xml文件写入输出流中。在(demo2.aspx运行时显示xml文档)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace XML1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = System.Text.Encoding.UTF8;//输入的编码
            settings.Indent = true;
            using (XmlWriter write = XmlWriter.Create(this.Response.Output, settings))
            {
                write.WriteStartDocument();//开始写
                write.WriteStartElement("books");
                write.WriteStartElement("book");//book节点开始
                write.WriteStartAttribute("id");//给book添加属性
                write.WriteValue("1");//设置属性id的值为1
                write.WriteEndAttribute();//结束属性
                write.WriteStartElement("author");//author节点

                write.WriteString("张闻正");//author节点中的内容
                write.WriteEndElement();//结束author节点
                write.WriteEndElement();//结束book节点

                //再添一本书
                write.WriteStartElement("book");//book开始
                write.WriteStartElement("author");//author节点
                write.WriteString("清华大学出版社");//author节点中的内容
                write.WriteEndElement();//结束author节点
                write.WriteEndElement();//结束book节点

                write.WriteEndElement();
            }
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值