對於XML
想必各位都比較了解
我也就不用費筆墨來描述它是什麼了
我想在未來的Web開發中XML一定會大放異彩
XML是可擴展標記語言
使用它企業可以制定一套自己的數據格式
數據按照這種格式在網絡中傳輸然後再通過XSLT將數據轉換成用戶期望的樣子表示出來
這樣便輕易的解決了數據格式不兼容的問題
用於Internet的數據傳輸
我想
這是XML對於我們這些程序員最誘人的地方!
我們今天的主題不是論述XML的好處
而是討論在C#中如何使用XML
下面我們來了解一下使用程序訪問XML的一些基礎理論知識
訪問的兩種模型
在程序中訪問進而操作XML文件一般有兩種模型
分別是使用DOM(文檔對象模型)和流模型
使用DOM的好處在於它允許編輯和更新XML文檔
可以隨機訪問文檔中的數據
可以使用XPath查詢
但是
DOM的缺點在於它需要一次性的加載整個文檔到內存中
對於大型的文檔
這會造成資源問題
流模型很好的解決了這個問題
因為它對XML文件的訪問采用的是流的概念
也就是說
任何時候在內存中只有當前節點
但它也有它的不足
它是只讀的
僅向前的
不能在文檔中執行向後導航操作
雖然是各有千秋
但我們也可以在程序中兩者並用實現優劣互補嘛
呵呵
這是題外話了!我們今天主要討論XML的讀取
那我們就詳細討論一下流模型吧!
流模型中的變體
流模型每次迭代XML文檔中的一個節點
適合於處理較大的文檔
所耗內存空間小
流模型中有兩種變體——
推
模型和
拉
模型
推模型也就是常說的SAX
SAX是一種靠事件驅動的模型
也就是說
它每發現一個節點就用推模型引發一個事件
而我們必須編寫這些事件的處理程序
這樣的做法非常的不靈活
也很麻煩
NET中使用的是基於
拉
模型的實現方案
拉
模型在遍歷文檔時會把感興趣的文檔部分從讀取器中拉出
不需要引發事件
允許我們以編程的方式訪問文檔
這大大的提高了靈活性
在性能上
拉
模型可以選擇性的處理節點
而SAX每發現一個節點都會通知客戶機
從而
使用
拉
模型可以提高Application的整體效率
在
NET中
拉
模型是作為XmlReader類實現的
下面看一下該類的繼承結構
我們今天來講一下該體系結構中的XmlTextReader類
該類提供對Xml文件進行讀取的功能
它可以驗證文檔是否格式良好
如果不是格式良好的Xml文檔
該類在讀取過程中將會拋出XmlException異常
可使用該類提供的一些方法對文檔節點進行讀取
篩選等操作以及得到節點的名稱和值
請牢記
XmlTextReader是基於流模型的實現
打個不恰當的比喻
XML文件就好象水源
閘一開水就流出
流過了就流過了不會也不可以往回流
內存中任何時候只有當前節點
你可以使用XmlTextReader類的Read()方法讀取下一個節點
好了
說了這麼多來看一個例子
編程要注重實際對吧
看代碼前先看下運行效果吧!
Example
按紐遍歷文檔讀取數據
Example
Example
按紐得到節點類型
Example
過濾文檔只獲得數據內容
Example
得到屬性節點
Example
按紐得到命名空間
Example
顯示整個XML文檔
為此
我專門寫一個類來封裝以上功能
該類代碼如下
//
//XmlReader類用於Xml文件的一般讀取操作
以下對這個類做簡單介紹
//
//Attributes(屬性):
//listBox: 設置該屬性主要為了得到客戶端控件以便於顯示所讀到的文件的內容(這裡是ListBox控件)
//xmlPath: 設置該屬性為了得到一個確定的Xml文件的絕對路徑
//
//Basilic Using(重要的引用):
//System
Xml: 該命名空間中封裝有對Xml進行操作的常用類
本類中使用了其中的XmlTextReader類
//XmlTextReader: 該類提供對Xml文件進行讀取的功能
它可以驗證文檔是否格式良好
如果不是格式 // 良好的Xml文檔
該類在讀取過程中將會拋出XmlException異常
可使用該類提供的
// 一些方法對文檔節點進行讀取
篩選等操作以及得到節點的名稱和值
//
//bool XmlTextReader
Read(): 讀取流中下一個節點
當讀完最後一個節點再次調用該方法該方法返回false
//XmlNodeType XmlTextReader
NodeType: 該屬性返回當前節點的類型
// XmlNodeType
Element 元素節點
// XmlNodeType
EndElement 結尾元素節點
// XmlNodeType
XmlDeclaration 文檔的第一個節點
// XmlNodeType
Text 文本節點
//bool XmlTextReader
HasAttributes: 當前節點有沒有屬性
返回true或false
//string XmlTextReader
Name: 返回當前節點的名稱
//string XmlTextReader
Value: 返回當前節點的值
//string XmlTextReader
LocalName: 返回當前節點的本地名稱
//string XmlTextReader
NamespaceURI: 返回當前節點的命名空間URI
//string XmlTextReader
Prefix: 返回當前節點的前綴
//bool XmlTextReader
MoveToNextAttribute(): 移動到當前節點的下一個屬性
//
namespace XMLReading
{
using System;
using System
Xml;
using System
Windows
Forms;
using System
ComponentModel;
/// <summary>
/// Xml文件讀取器
/// </summary>
public class XmlReader : IDisposable
{
private string _xmlPath;
private const string _errMsg =
Error Occurred While Reading
;
private ListBox _listBox;
private XmlTextReader xmlTxtRd;
#region XmlReader 的構造器
public XmlReader()
{
this
_xmlPath = string
Empty;
this
_listBox = null;
this
xmlTxtRd = null;
}
/// <summary>
/// 構造器
/// </summary>
/// <param name=
_xmlPath
>xml文件絕對路徑</param>
/// <param name=
_listBox
>列表框用於顯示xml</param>
public XmlReader(string _xmlPath
ListBox _listBox)
{
this
_xmlPath = _xmlPath;
this
_listBox = _listBox;
this
xmlTxtRd = null;
}
#endregion
#region XmlReader 的資源釋放方法
/// <summary>
/// 清理該對象所有正在使用的資源
/// </summary>
public void Dispose()
{
this
Dispose(true);
GC
SuppressFinalize(this);
}
/// <summary>
/// 釋放該對象的實例變量
/// </summary>
/// <param name=
disposing
></param>
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
if (this
xmlTxtRd != null)
{
this
xmlTxtRd
Close();
this
xmlTxtRd = null;
}
if (this
_xmlPath != null)
{
this
_xmlPath = null;
}
}
#endregion
#region XmlReader 的屬性
/// <summary>
/// 獲取或設置列表框用於顯示xml
/// </summary>
public ListBox listBox
{
get
{
return this
_listBox;
}
set
{
this
_listBox = value;
}
}
/// <summary>
/// 獲取或設置xml文件的絕對路徑
/// </summary>
public string xmlPath
{
get
{
return this
_xmlPath;
}
set
{
this
_xmlPath = value;
}
}
#endregion
/// <summary>
/// 遍歷Xml文件
/// </summary>
public void EachXml()
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
this
_listBox
Items
Add(this
xmlTxtRd
Value);
}
}
catch(XmlException exp)
{
throw new XmlException(_errMsg + this
_xmlPath + exp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 讀取Xml文件的節點類型
/// </summary>
public void ReadXmlByNodeType()
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
this
_listBox
Items
Add(this
xmlTxtRd
NodeType
ToString());
}
}
catch(XmlException exp)
{
throw new XmlException(_errMsg + this
_xmlPath + exp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 根據節點類型過濾Xml文檔
/// </summary>
/// <param name=
xmlNType
>XmlNodeType 節點類型的數組</param>
public void FilterByNodeType(XmlNodeType[] xmlNType)
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
for (int i =
; i < xmlNType
Length; i++)
{
if (xmlTxtRd
NodeType == xmlNType[i])
{
this
_listBox
Items
Add(xmlTxtRd
Name +
is Type
+ xmlTxtRd
NodeType
ToString());
}
}
}
}
catch(XmlException exp)
{
throw new XmlException(_errMsg + this
xmlPath + exp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 讀取Xml文件的所有文本節點值
/// </summary>
public void ReadXmlTextValue()
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
if (xmlTxtRd
NodeType == XmlNodeType
Text)
{
this
_listBox
Items
Add(xmlTxtRd
Value);
}
}
}
catch(XmlException xmlExp)
{
throw new XmlException(_errMsg + this
_xmlPath + xmlExp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 讀取Xml文件的屬性
/// </summary>
public void ReadXmlAttributes()
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
if (xmlTxtRd
NodeType == XmlNodeType
Element)
{
if (xmlTxtRd
HasAttributes)
{
this
_listBox
Items
Add(
The Element
+ xmlTxtRd
Name +
has
+ xmlTxtRd
AttributeCount +
Attributes
);
this
_listBox
Items
Add(
The Attributes are:
);
while(xmlTxtRd
MoveToNextAttribute())
{
this
_listBox
Items
Add(xmlTxtRd
Name +
=
+ xmlTxtRd
Value);
}
}
else
{
this
_listBox
Items
Add(
The Element
+ xmlTxtRd
Name +
has no Attribute
);
}
this
_listBox
Items
Add(
);
}
}
}
catch(XmlException xmlExp)
{
throw new XmlException(_errMsg + this
_xmlPath + xmlExp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 讀取Xml文件的命名空間
/// </summary>
public void ReadXmlNamespace()
{
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
if (xmlTxtRd
NodeType == XmlNodeType
Element && xmlTxtRd
Prefix !=
)
{
this
_listBox
Items
Add(
The Prefix
+ xmlTxtRd
Prefix +
is associated with namespace
+ xmlTxtRd
NamespaceURI);
this
_listBox
Items
Add(
The Element with the local name
+ xmlTxtRd
LocalName +
is associated with
+
the namespace
+ xmlTxtRd
NamespaceURI);
}
if (xmlTxtRd
NodeType == XmlNodeType
Element && xmlTxtRd
HasAttributes)
{
while(xmlTxtRd
MoveToNextAttribute())
{
if (xmlTxtRd
Prefix !=
)
{
this
_listBox
Items
Add(
The Prefix
+ xmlTxtRd
Prefix +
is associated with namespace
+ xmlTxtRd
NamespaceURI);
this
_listBox
Items
Add(
The Attribute with the local name
+ xmlTxtRd
LocalName +
is associated with the namespace
+ xmlTxtRd
NamespaceURI);
}
}
}
}
}
catch(XmlException xmlExp)
{
throw new XmlException(_errMsg + this
_xmlPath + xmlExp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
/// <summary>
/// 讀取整個Xml文件
/// </summary>
public void ReadXml()
{
string attAndEle = string
Empty;
this
_listBox
Items
Clear();
this
xmlTxtRd = new XmlTextReader(this
_xmlPath);
try
{
while(xmlTxtRd
Read())
{
if (xmlTxtRd
NodeType == XmlNodeType
XmlDeclaration)
this
_listBox
Items
Add(string
Format(
<?{
} {
} ?>
xmlTxtRd
Name
xmlTxtRd
Value));
else if (xmlTxtRd
NodeType == XmlNodeType
Element)
{
attAndEle = string
Format(
<{
}
xmlTxtRd
Name);
if (xmlTxtRd
HasAttributes)
{
while(xmlTxtRd
MoveToNextAttribute())
{
attAndEle = attAndEle + string
Format(
{
}=
{
}
xmlTxtRd
Name
xmlTxtRd
Value);
}
}
attAndEle = attAndEle
Trim() +
>
;
this
_listBox
Items
Add(attAndEle);
}
else if (xmlTxtRd
NodeType == XmlNodeType
EndElement)
this
_listBox
Items
Add(string
Format(
</{
}>
xmlTxtRd
Name));
else if (xmlTxtRd
NodeType == XmlNodeType
Text)
this
_listBox
Items
Add(xmlTxtRd
Value);
}
}
catch(XmlException xmlExp)
{
throw new XmlException(_errMsg + this
_xmlPath + xmlExp
ToString());
}
finally
{
if (this
xmlTxtRd != null)
this
xmlTxtRd
Close();
}
}
}
}
窗體代碼如下
namespace XMLReading
{
using System;
using System
Drawing;
using System
Collections;
using System
ComponentModel;
using System
Windows
Forms;
using System
Data;
using System
Xml;
public class Form
: System
Windows
Forms
Form
{
private System
Windows
Forms
ListBox listBox
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private System
Windows
Forms
Button button
;
private string xmlPath;
private XmlReader xRead;
/// <summary>
/// 必需的設計器變量
/// </summary>
private System
ComponentModel
Container components = null;
public Form
()
{
InitializeComponent();
}
/// <summary>
/// 清理所有正在使用的資源
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components
Dispose();
}
}
base
Dispose( disposing );
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法
不要使用代碼編輯器修改
/// 此方法的內容
/// </summary>
private void InitializeComponent()
{
this
listBox
= new System
Windows
Forms
ListBox();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
button
= new System
Windows
Forms
Button();
this
SuspendLayout();
//
// listBox
//
this
listBox
Anchor = ((System
Windows
Forms
AnchorStyles)((((System
Windows
Forms
AnchorStyles
Top | System
Windows
Forms
AnchorStyles
Bottom)
| System
Windows
Forms
AnchorStyles
Left)
| System
Windows
Forms
AnchorStyles
Right)));
this
listBox
ItemHeight =
;
this
listBox
Location = new System
Drawing
Point(
);
this
listBox
Name =
listBox
;
this
listBox
Size = new System
Drawing
Size(
);
this
listBox
TabIndex =
;
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Right)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// button
//
this
button
Anchor = ((System
Windows
Forms
AnchorStyles)((System
Windows
Forms
AnchorStyles
Bottom | System
Windows
Forms
AnchorStyles
Left)));
this
button
Location = new System
Drawing
Point(
);
this
button
Name =
button
;
this
button
TabIndex =
;
this
button
Text =
Example
;
this
button
Click += new System
EventHandler(this
button
_Click);
//
// Form
//
this
AutoScaleBaseSize = new System
Drawing
Size(
);
this
ClientSize = new System
Drawing
Size(
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
button
);
this
Controls
Add(this
listBox
);
this
Name =
Form
;
this
Text =
XMLReader
;
this
ResumeLayout(false);
//
// xmlPath
//
this
xmlPath =
sample
xml
;
}
#endregion
/// <summary>
/// 應用程序的主入口點
/// </summary>
[STAThread]
static void Main()
{
Application
Run(new Form
());
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
EachXml();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
ReadXmlByNodeType();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
XmlNodeType[] xmlNType = {XmlNodeType
Element
XmlNodeType
EndElement
XmlNodeType
XmlDeclaration};
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
FilterByNodeType(xmlNType);
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
ReadXmlTextValue();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
ReadXmlAttributes();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
ReadXmlNamespace();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
private void button
_Click(object sender
System
EventArgs e)
{
xRead = new XmlReader(this
xmlPath
this
listBox
);
try
{
xRead
ReadXml();
}
catch(XmlException xmlExp)
{
MessageBox
Show(xmlExp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
catch(Exception exp)
{
MessageBox
Show(exp
ToString()
Error
MessageBoxButtons
OK
MessageBoxIcon
Error);
}
finally
{
xRead
Dispose();
}
}
}
}
以下是用於測試的XML文件
在項目中新建一個XML文件取名為sample
xml
建好後把該文件拷到項目的bin目錄下的Debug目錄下
<?xml version=
encoding=
utf
?>
<Invoices date=
/
/
xmlns:cat=
uri:Business
Categories
>
<customers custname=
Chile Wines Inc
phone=
email=
custid=
delivery=
international
offerID=
>
<cat:businfo>Wine Division South</cat:businfo>
<cat:businfo>Beer Division North</cat:businfo>
<order orderID=
OID
batchid=
>
<details>
<items cat:num=
>
<item>Rancagua While</item>
<item>Rancagua Red</item>
</items>
</details>
</order>
<order orderID=
OID
>
<details>
<items num=
>
<item>Chillan Red</item>
<item>Rancagua While</item>
<item>Santiago Red</item>
<item>Rancagua While</item>
<item>Rancagua Red</item>
</items>
</details>
</order>
<order orderID=
OID
batchid=
>
<details>
<items num=
>
<item>Rancegao Red</item>
<item>Sutothad Black</item>
<item>BlackNme Blue</item>
<item>Booklist Red</item>
<item>Rancegao White</item>
</items>
</details>
</order>
</customers>
</Invoices>
From:http://tw.wingwit.com/Article/program/net/201311/12170.html