首页 > Windows开发 > 详细

c# 操作xml之xmlReader

时间:2016-03-16 17:14:47      阅读:206      评论:0      收藏:0      [点我收藏+]

xmlReader的名称空间using System.Xml;

xmlReader是通过流的方式来读取xml文件的内容

<?xml version="1.0" encoding="utf-8" ?>
<!--<!-–This file represents a fragment of a book store inventory database-–>-->
<bookstore>
  <book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
    <title>The Autobiography of Benjamin Franklin</title>

</bookstore>

可以通过下面方式来读取<title>元素中的内容

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace xmlTest
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlReader rdr = XmlReader.Create("books.xml");
            while(rdr.Read())
            {
                if(rdr.NodeType == XmlNodeType.Text)
                {
                    string str = rdr.Value;
                }
            }
        }
    }
}

通过设置断点会发现xmlReader是一步一步从xml中读取文件内容的,其中空格、注释等都会读取

c# 操作xml之xmlReader

原文:http://www.cnblogs.com/tianmochou/p/5283911.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!