欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-24 17:26:40
xml源文件为note.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();
        }
    }
}

运行结果如下:

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