淺析C#如何使用XML
C#如何使用XML
對(duì)于XML,想必各位都比較了解,我也就不用費(fèi)筆墨來(lái)描述它是什么了,我想在未來(lái)的Web開(kāi)發(fā)中XML一定會(huì)大放異彩,XML是可擴(kuò)展標(biāo)記語(yǔ)言,使用它企業(yè)可以制定一套自己的數(shù)據(jù)格式,數(shù)據(jù)按照這種格式在網(wǎng)絡(luò)中傳輸然后再通過(guò)XSLT將數(shù)據(jù)轉(zhuǎn)換成用戶期望的樣子表示出來(lái),這樣便輕易的解決了數(shù)據(jù)格式不兼容的問(wèn)題。用于Internet的數(shù)據(jù)傳輸,我想,這是XML對(duì)于我們這些程序員最誘人的地方!
我們今天的主題不是論述XML的好處,而是討論在C#如何使用XML。下面我們來(lái)了解一下使用程序訪問(wèn)XML的一些基礎(chǔ)理論知識(shí)。
訪問(wèn)的兩種模型:
在程序中訪問(wèn)進(jìn)而操作XML文件一般有兩種模型,分別是使用DOM(文檔對(duì)象模型)和流模型,使用DOM的好處在于它允許編輯和更新XML文檔,可以隨機(jī)訪問(wèn)文檔中的數(shù)據(jù),可以使用XPath查詢,但是,DOM的缺點(diǎn)在于它需要一次性的加載整個(gè)文檔到內(nèi)存中,對(duì)于大型的文檔,這會(huì)造成資源問(wèn)題。流模型很好的解決了這個(gè)問(wèn)題,因?yàn)樗鼘?duì)XML文件的訪問(wèn)采用的是流的概念,也就是說(shuō),任何時(shí)候在內(nèi)存中只有當(dāng)前節(jié)點(diǎn),但它也有它的不足,它是只讀的,僅向前的,不能在文檔中執(zhí)行向后導(dǎo)航操作。雖然是各有千秋,但我們也可以在程序中兩者并用實(shí)現(xiàn)優(yōu)劣互補(bǔ)嘛,呵呵,這是題外話了!我們今天主要討論XML的讀取,那我們就詳細(xì)討論一下流模型吧!
流模型中的變體:
流模型每次迭代XML文檔中的一個(gè)節(jié)點(diǎn),適合于處理較大的文檔,所耗內(nèi)存空間小。流模型中有兩種變體——“推”模型和“拉”模型。
推模型也就是常說(shuō)的SAX,SAX是一種靠事件驅(qū)動(dòng)的模型,也就是說(shuō):它每發(fā)現(xiàn)一個(gè)節(jié)點(diǎn)就用推模型引發(fā)一個(gè)事件,而我們必須編寫(xiě)這些事件的處理程序,這樣的做法非常的不靈活,也很麻煩。
.NET中使用的是基于“拉”模型的實(shí)現(xiàn)方案,“拉”模型在遍歷文檔時(shí)會(huì)把感興趣的文檔部分從讀取器中拉出,不需要引發(fā)事件,允許我們以編程的方式訪問(wèn)文檔,這大大的提高了靈活性,在性能上“拉”模型可以選擇性的處理節(jié)點(diǎn),而SAX每發(fā)現(xiàn)一個(gè)節(jié)點(diǎn)都會(huì)通知客戶機(jī),從而,使用“拉”模型可以提高 Application的整體效率。
我們今天來(lái)講一下該體系結(jié)構(gòu)中的XmlTextReader類,該類提供對(duì)Xml文件進(jìn)行讀取的功能,它可以驗(yàn)證文檔是否格式良好,如果不是格式良好的Xml文檔,該類在讀取過(guò)程中將會(huì)拋出XmlException異常,可使用該類提供的一些方法對(duì)文檔節(jié)點(diǎn)進(jìn)行讀取,篩選等操作以及得到節(jié)點(diǎn)的名稱和值,請(qǐng)牢記:XmlTextReader是基于流模型的實(shí)現(xiàn),打個(gè)不恰當(dāng)?shù)谋扔?,XML文件就好象水源,閘一開(kāi)水就流出,流過(guò)了就流過(guò)了不會(huì)也不可以往回流。內(nèi)存中任何時(shí)候只有當(dāng)前節(jié)點(diǎn),你可以使用XmlTextReader類的Read()方法讀取下一個(gè)節(jié)點(diǎn)。好了,說(shuō)了這么多來(lái)看一個(gè)例子,編程要注重實(shí)際對(duì)吧。
Example1按紐遍歷文檔讀取數(shù)據(jù),Example2,Example3按紐得到節(jié)點(diǎn)類型,Example4過(guò)濾文檔只獲得數(shù)據(jù)內(nèi)容,Example5得到屬性節(jié)點(diǎn),Example6按紐得到命名空間,Example7顯示整個(gè)XML文檔,為此,我專門(mén)寫(xiě)一個(gè)類來(lái)封裝以上功能,該類代碼如下:
- 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 的構(gòu)造器
- public XmlReader()
- {
- this._xmlPath = string.Empty;
- this._listBox = null;
- this.xmlTxtRd = null;
- }
- /// <summary>
- /// 構(gòu)造器
- /// </summary>
- /// <param name="_xmlPath">xml文件絕對(duì)路徑</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>
- /// 清理該對(duì)象所有正在使用的資源
- /// </summary>
- public void Dispose()
- {
- this.Dispose(true);
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// 釋放該對(duì)象的實(shí)例變量
- /// </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>
- /// 獲取或設(shè)置列表框用于顯示xml
- /// </summary>
- public ListBox listBox
- {
- get
- {
- return this._listBox;
- }
- set
- {
- this._listBox = value;
- }
- }
- /// <summary>
- /// 獲取或設(shè)置xml文件的絕對(duì)路徑
- /// </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文件的節(jié)點(diǎn)類型
- /// </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>
- /// 根據(jù)節(jié)點(diǎn)類型過(guò)濾Xml文檔
- /// </summary>
- /// <param name="xmlNType">XmlNodeType 節(jié)點(diǎn)類型的數(shù)組</param>
- public void FilterByNodeType(XmlNodeType[] xmlNType)
- {
- this._listBox.Items.Clear();
- this.xmlTxtRd = new XmlTextReader(this._xmlPath);
- try
- {
- while(xmlTxtRd.Read())
- {
- for (int i = 0; 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文件的所有文本節(jié)點(diǎn)值
- /// </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>
- /// 讀取整個(gè)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("<?{0} {1} ?>",
xmlTxtRd.Name,xmlTxtRd.Value));- else if (xmlTxtRd.NodeType == XmlNodeType.Element)
- {
- attAndEle = string.Format("<{0} ",xmlTxtRd.Name);
- if (xmlTxtRd.HasAttributes)
- {
- while(xmlTxtRd.MoveToNextAttribute())
- {
- attAndEleattAndEle = attAndEle + string.Format("{0}='{1}'
",xmlTxtRd.Name,xmlTxtRd.Value);- }
- }
- attAndEleattAndEle = attAndEle.Trim() + ">";
- this._listBox.Items.Add(attAndEle);
- }
- else if (xmlTxtRd.NodeType == XmlNodeType.EndElement)
- this._listBox.Items.Add(string.Format("</{0}>",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();
- }
- }
- }
- }
以上介紹C#如何使用XML
【編輯推薦】