2010年12月30日 星期四

讀取XML文件

使用XmlTextReader類別讀取XML文件,這個類別會一行一行讀取直到檔案結束。
XmlTextReader類別的NodeType屬性,用來判斷節點類型。
books.xml

<catalog>
<book isbn="1-56592-724-9">
<title>The Cathedral &amp; the Bazaar</title>
<author>Eric S. Raymond</author>
</book>
<book isbn="1-56592-051-1">
<title>Making TeX Work</title>
<author>Norman Walsh</author>
</book>
</catalog>

程式碼
using System;
using System.Xml;

namespace ReadXml1
{
class Class1
{
static void Main(string[] args)
{
// Create an isntance of XmlTextReader and call Read method to read the file
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
textReader.Read();

// If the node has value
while (textReader.Read())
{
// Move to fist element
textReader.MoveToElement();
Console.WriteLine("XmlTextReader Properties Test");
Console.WriteLine("===================");
// Read this element's properties and display them on console
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("Depth:" + textReader.Depth.ToString());
Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
}
Console.ReadLine ();
}
}
}
輸出
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:1
Line Number:2
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:book
Base URI:file:///C:/books.xml
Local Name:book
Attribute Count:1
Depth:1
Line Number:3
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:3
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:title
Base URI:file:///C:/books.xml
Local Name:title
Attribute Count:0
Depth:2
Line Number:4
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:3
Line Number:4
Node Type:Text
Attribute Count:The Cathedral & the Bazaar
XmlTextReader Properties Test
===================
Name:title
Base URI:file:///C:/books.xml
Local Name:title
Attribute Count:0
Depth:2
Line Number:4
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:4
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:author
Base URI:file:///C:/books.xml
Local Name:author
Attribute Count:0
Depth:2
Line Number:5
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:3
Line Number:5
Node Type:Text
Attribute Count:Eric S. Raymond
XmlTextReader Properties Test
===================
Name:author
Base URI:file:///C:/books.xml
Local Name:author
Attribute Count:0
Depth:2
Line Number:5
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:5
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:book
Base URI:file:///C:/books.xml
Local Name:book
Attribute Count:0
Depth:1
Line Number:6
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:1
Line Number:6
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:book
Base URI:file:///C:/books.xml
Local Name:book
Attribute Count:1
Depth:1
Line Number:7
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:7
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:title
Base URI:file:///C:/books.xml
Local Name:title
Attribute Count:0
Depth:2
Line Number:8
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:3
Line Number:8
Node Type:Text
Attribute Count:Making TeX Work
XmlTextReader Properties Test
===================
Name:title
Base URI:file:///C:/books.xml
Local Name:title
Attribute Count:0
Depth:2
Line Number:8
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:8
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:author
Base URI:file:///C:/books.xml
Local Name:author
Attribute Count:0
Depth:2
Line Number:9
Node Type:Element
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:3
Line Number:9
Node Type:Text
Attribute Count:Norman Walsh
XmlTextReader Properties Test
===================
Name:author
Base URI:file:///C:/books.xml
Local Name:author
Attribute Count:0
Depth:2
Line Number:9
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:2
Line Number:9
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:book
Base URI:file:///C:/books.xml
Local Name:book
Attribute Count:0
Depth:1
Line Number:10
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:1
Line Number:10
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:1
Line Number:11
Node Type:Comment
Attribute Count: imagine more entries here..
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:1
Line Number:11
Node Type:Whitespace
Attribute Count:
XmlTextReader Properties Test
===================
Name:catalog
Base URI:file:///C:/books.xml
Local Name:catalog
Attribute Count:0
Depth:0
Line Number:12
Node Type:EndElement
Attribute Count:
XmlTextReader Properties Test
===================
Name:
Base URI:file:///C:/books.xml
Local Name:
Attribute Count:0
Depth:0
Line Number:12
Node Type:Whitespace
Attribute Count:



讀取XML文件,找出節點類型並將之印出。
程式碼
using System;
using System.Xml;
namespace ReadingXML2
{
class Class1
{
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;
// Read a document
XmlTextReader textReader = new XmlTextReader("C:\\books.xml");
// Read until end of file
while (textReader.Read())
{
XmlNodeType nType = textReader.NodeType;
// If node type us a declaration
if (nType == XmlNodeType.XmlDeclaration)
{
Console.WriteLine("Declaration:" + textReader.Name.ToString());
xd = xd + 1;
}
// if node type is a comment
if (nType == XmlNodeType.Comment)
{
Console.WriteLine("Comment:" + textReader.Name.ToString());
cc = cc + 1;
}
// if node type us an attribute
if (nType == XmlNodeType.Attribute)
{
Console.WriteLine("Attribute:" + textReader.Name.ToString());
ac = ac + 1;
}
// if node type is an element
if (nType == XmlNodeType.Element)
{
Console.WriteLine("Element:" + textReader.Name.ToString());
el = el + 1;
}
// if node type is an entity\
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
et = et + 1;
}
// if node type is a Process Instruction
if (nType == XmlNodeType.Entity)
{
Console.WriteLine("Entity:" + textReader.Name.ToString());
pi = pi + 1;
}
// if node type a document
if (nType == XmlNodeType.DocumentType)
{
Console.WriteLine("Document:" + textReader.Name.ToString());
dc = dc + 1;
}
// if node type is white space
if (nType == XmlNodeType.Whitespace)
{
Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
ws = ws + 1;
}
}
// Write the summary
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());
Console.ReadLine ();
}
}
}
輸出
WhiteSpace:
Element:catalog
WhiteSpace:
Element:book
WhiteSpace:
Element:title
WhiteSpace:
Element:author
WhiteSpace:
WhiteSpace:
Element:book
WhiteSpace:
Element:title
WhiteSpace:
Element:author
WhiteSpace:
WhiteSpace:
Comment:
WhiteSpace:
WhiteSpace:
Total Comments:1
Total Attributes:0
Total Elements:7
Total Entity:0
Total Process Instructions:0
Total Declaration:0
Total DocumentType:0
Total WhiteSpaces:12

沒有留言: