zoukankan      html  css  js  c++  java
  • 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");

            }
        }
    }

  • 相关阅读:
    “LM/w3svc/1/root /***” 别名已存在
    Dawn of a New Day
    线程池(java.util.concurrent.ThreadPoolExecutor)的使用
    放心走吧,谷歌中国
    实现MySQL允许远程连接
    Google Engineer Gets $6 Million For Not Going To Facebook
    mysql主从同步出现Slave_IO_Running: Connecting的解决思路
    解决eclipse/sts加入@Controller注解后alt+/快捷键的提示功能失效
    Maven异常:Missing artifact org.slf4j:slf4japi:jar:1.7.25以及properties标签作用
    eclipse和sts使用alt+/代码提示,有两个相同的提示
  • 原文地址:https://www.cnblogs.com/poiu/p/2827168.html
Copyright © 2011-2022 走看看