//--------------------------------------------------------------------------------------------------- //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 Oclearcase/" target="_blank" >ccurred 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 = 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文件的所有文本節點值 /// </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("<?{0} {1} ?>",xmlTxtRd.Name,xmlTxtRd.Value)); else if (xmlTxtRd.NodeType == XmlNodeType.Element) { attAndEle = string.Format("<{0} ",xmlTxtRd.Name); if (xmlTxtRd.HasAttributes) { while(xmlTxtRd.MoveToNextAttribute()) { attAndEle = attAndEle + string.Format("{0}='{1}' ",xmlTxtRd.Name,xmlTxtRd.Value); } } attAndEle = 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(); } } } } |
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 Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button5; private System.Windows.Forms.Button button6; private System.Windows.Forms.Button button7; private string xmlPath; private XmlReader xRead; /// <summary> /// 必需的設計器變量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { 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.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.button5 = new System.Windows.Forms.Button(); this.button6 = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // listBox1 // this.listBox1.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.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(8, 8); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(716, 460); this.listBox1.TabIndex = 0; // // button1 // this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button1.Location = new System.Drawing.Point(8, 488); this.button1.Name = "button1"; this.button1.TabIndex = 1; this.button1.Text = "Example1"; this.button1.Click += new System.EventHandler(this.button1_Click); // // button2 // this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button2.Location = new System.Drawing.Point(96, 488); this.button2.Name = "button2"; this.button2.TabIndex = 2; this.button2.Text = "Example2"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.button3.Location = new System.Drawing.Point(648, 488); this.button3.Name = "button3"; this.button3.TabIndex = 3; this.button3.Text = "Example7"; this.button3.Click += new System.EventHandler(this.button3_Click); // // button4 // this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button4.Location = new System.Drawing.Point(184, 488); this.button4.Name = "button4"; this.button4.TabIndex = 4; this.button4.Text = "Example3"; this.button4.Click += new System.EventHandler(this.button4_Click); // // button5 // this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button5.Location = new System.Drawing.Point(272, 488); this.button5.Name = "button5"; this.button5.TabIndex = 5; this.button5.Text = "Example4"; this.button5.Click += new System.EventHandler(this.button5_Click); // // button6 // this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button6.Location = new System.Drawing.Point(360, 488); this.button6.Name = "button6"; this.button6.TabIndex = 6; this.button6.Text = "Example5"; this.button6.Click += new System.EventHandler(this.button6_Click); // // button7 // this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.button7.Location = new System.Drawing.Point(448, 488); this.button7.Name = "button7"; this.button7.TabIndex = 7; this.button7.Text = "Example6"; this.button7.Click += new System.EventHandler(this.button7_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(728, 517); this.Controls.Add(this.button7); this.Controls.Add(this.button6); this.Controls.Add(this.button5); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); this.Controls.Add(this.button1); this.Controls.Add(this.listBox1); this.Name = "Form1"; this.Text = "XMLReader"; this.ResumeLayout(false); // // xmlPath // this.xmlPath = "sample.xml"; } #endregion /// <summary> /// 應用程序的主入口點。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath,this.listBox1); 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 button2_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath,this.listBox1); 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 button3_Click(object sender, System.EventArgs e) { XmlNodeType[] xmlNType = {XmlNodeType.Element, XmlNodeType.EndElement, XmlNodeType.XmlDeclaration}; xRead = new XmlReader(this.xmlPath, this.listBox1); 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 button4_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath, this.listBox1); 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 button5_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath, this.listBox1); 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 button6_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath, this.listBox1); 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 button7_Click(object sender, System.EventArgs e) { xRead = new XmlReader(this.xmlPath, this.listBox1); 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 version="1.0" encoding="utf-8" ?> <Invoices date="28/11/2001" xmlns:cat="uri:Business-Categories"> <customers custname="Chile Wines Inc" phone="1241 923 56332" email="cw@yahoo.com.cn" custid="192398" delivery="international" offerID="27"> <cat:businfo>Wine Division South</cat:businfo> <cat:businfo>Beer Division North</cat:businfo> <order orderID="OID921" batchid="123"> <details> <items cat:num="2"> <item>Rancagua While</item> <item>Rancagua Red</item> </items> </details> </order> <order orderID="OID927"> <details> <items num="5"> <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="OID931" batchid="123"> <details> <items num="6"> <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> |
原文轉自:http://www.anti-gravitydesign.com