應用xmltextreader對象讀取xml文檔示例。本站提示廣大學習愛好者:(應用xmltextreader對象讀取xml文檔示例)文章只能為提供參考,不一定能成為您想要的結果。以下是應用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();
}
}
}
運轉成果以下: