亲宝软件园·资讯

展开

读取xml文档 使用xmltextreader对象读取xml文档示例

人气:1
想了解使用xmltextreader对象读取xml文档示例的相关内容吗,在本文为您仔细讲解读取xml文档的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:xmltextreader,读取xml,下面大家一起来学习吧。

XML源文件为note.xml

复制代码 代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
</note>

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;   //引入命名空间
namespace ReadXmlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //打开xml
            XmlTextReader textReader = new XmlTextReader("C:\\note.xml");
            //读取数据
            while (textReader.Read())
            {
                textReader.MoveToElement();
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("==============================");
                Console.WriteLine("Name:"+textReader.Name);
                Console.WriteLine("Base URI:"+textReader.BaseURI);
                Console.WriteLine("Local Name:"+textReader.LocalName);
                Console.WriteLine("Attribute Count:"+textReader.AttributeCount.ToString());
                Console.WriteLine("Line Number:"+textReader.LineNumber.ToString());
                Console.WriteLine("Node Type:"+textReader.NodeType.ToString());
                Console.WriteLine("Attribute Count:"+textReader.Value.ToString());
            }
            Console.ReadKey();
        }
    }
}

运行结果如下:

加载全部内容

相关教程
猜你喜欢
用户评论