使用xmltextreader对象读取xml文档示例
程序员文章站
2023-12-20 16:11:58
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();
}
}
}
运行结果如下: