using System;
using System.Xml;
namespace ReadXML
{
class Class2
{
static void Main( string[] args )
{
int ws = 0;
int pi = 0;
int dc = 0;
int cc = 0;
int ac = 0;
int et = 0;
int el = 0;
int xd = 0;
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// 节点类型为XmlDeclaration
if (nType == XmlNodeType.XmlDeclaration)
{
Console.WriteLine("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
// 节点类型为Comment
if( nType == XmlNodeType.Comment)
{
Console.WriteLine("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
// 节点类型为Attribute
if( nType == XmlNodeType.Attribute)
{
Console.WriteLine("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
// 节点类型为Element
if ( nType == XmlNodeType.Element)
{
Console.WriteLine("Element:" + textReader.Name.ToString());
el = el + 1;
}
// 节点类型为Entity
if ( nType == XmlNodeType.Entity )
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
et = et + 1;
}
// 节点类型为Process Instruction
if( nType == XmlNodeType. ProcessInstruction )
{
Console.WriteLine("Process Instruction:" + textReader.Name.ToString());
pi = pi + 1;
}
// 节点类型为DocumentType
if( nType == XmlNodeType.DocumentType)
{
Console.WriteLine("DocumentType:" + textReader.Name.ToString());
dc = dc + 1;
}
// 节点类型为Whitespace
if ( nType == XmlNodeType.Whitespace )
{
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
}
// 在控制台中显示每种类型的数目
Console.WriteLine("Total Comments:" + cc.ToString());
Console.WriteLine("Total Attributes:" + ac.ToString());
Console.WriteLine("Total Elements:" + el.ToString());
Console.WriteLine("Total Entity:" + et.ToString());
Console.WriteLine("Total Process Instructions:" + pi.ToString());
Console.WriteLine("Total Declaration:" + xd.ToString());
Console.WriteLine("Total DocumentType:" + dc.ToString());
Console.WriteLine("Total WhiteSpaces:" + ws.ToString());
}
}
}