




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、Good is good, but better carries it.精益求精,善益求善。C讀取XML文件經(jīng)典案例-讀取XML到ListBox/ComboBox1,知識需求:(1)訪問XML文件的兩個基本模型:DOM模型;使用DOM的好處在于它允許編輯和更新XML文檔,可以隨機訪問文檔中的數(shù)據(jù),可以使用XPath查詢,但是,DOM的缺點在于它需要一次性的加載整個文檔到內(nèi)存中,對于大型的文檔,這會造成資源問題。流模型;流模型很好的解決了這個問題,因為它對XML文件的訪問采用的是流的概念,也就是說,任何時候在內(nèi)存中只有當(dāng)前節(jié)點,但它也有它的不足,它是只讀的,僅向前的,不能在文檔中執(zhí)行向后導(dǎo)航操作
2、。雖然是各有千秋,但我們也可以在程序中兩者并用實現(xiàn)優(yōu)劣互補。C#采用流模型。流模型每次迭代XML文檔中的一個節(jié)點,適合于處理較大的文檔,所耗內(nèi)存空間小。流模型中有兩種變體“推”push模型和pull“拉”模型。推模型也就是常說的SAX,SAX是一種靠事件驅(qū)動的模型,也就是說:它每發(fā)現(xiàn)一個節(jié)點就用推模型引發(fā)一個事件,而我們必須編寫這些事件的處理程序,這樣的做法非常的不靈活,也很麻煩。.NET中使用的是基于“拉”模型的實現(xiàn)方案,“拉”模型在遍歷文檔時會把感興趣的文檔部分從讀取器中拉出,不需要引發(fā)事件,允許我們以編程的方式訪問文檔,這大大的提高了靈活性,在性能上“拉”模型可以選擇性的處理節(jié)點,而SA
3、X每發(fā)現(xiàn)一個節(jié)點都會通知客戶機,從而,使用“拉”模型可以提高Application的整體效率。在.NET中“拉”模型是作為XmlReader類(抽象類)實現(xiàn)的(2)XmlReader類Representsareaderthatprovidesfast,non-cached,forward-onlyaccesstoXMLdata.該類中有三個重要的衍生類:XmlTextReader;XmlTextValidatingReader;XmlNodeReader(3)XmlNodeType枚舉該枚舉里面有很多實用的數(shù)。2,案例(VS2008+XML)Form1.cs*usingSystem;using
4、System.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Xml;namespaceDemoXmlReaderpublicpartialclassXmlForm:FormpublicXmlForm()InitializeComponent();privatevoidbtnReaderXML_Click(objectsender,E
5、ventArgse)XmlReaderxr=newXmlReader(txtPath.Text,lbxml);try/xr.EachXmlToListBox();xr.ReadXmlTextToListBox();/xr.ReadXml();catch(XmlExceptionxe)MessageBox.Show(xe.ToString(),Error,MessageBoxButtons.OK,MessageBoxIcon.Error);catch(Exceptionexp)MessageBox.Show(exp.ToString(),Error,MessageBoxButtons.OK,Me
6、ssageBoxIcon.Error);finallyxr.Dispose();privatevoidbutton1_Click(objectsender,EventArgse)OpenFileDialogofd=newOpenFileDialog();ofd.InitialDirectory=Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);/指定默認打開的窗口指向的文件ofd.ShowDialog();txtPath.Text=ofd.FileName;/把路徑復(fù)制給txtPath文本框privatevoidbt
7、nReaderXmlToCb_Click(objectsender,EventArgse)XmlReaderxr=newXmlReader(txtPath.Text,cbxml);tryxr.EachXmlToComboBox();catch(XmlExceptionxe)MessageBox.Show(xe.ToString(),Error,MessageBoxButtons.OK,MessageBoxIcon.Error);catch(Exceptionexp)MessageBox.Show(exp.ToString(),Error,MessageBoxButtons.OK,Message
8、BoxIcon.Error);finallyxr.Dispose();classXmlReader:IDisposableprivatestringxmlPath;privateconststringERRMSG=ErrorocurredWhileReading;privateListBoxlistbox;privateComboBoxcombobox;privateXmlTextReaderxtr;#regionXmlReader構(gòu)造器publicXmlReader()this.xmlPath=string.Empty;this.xtr=null;this.listbox=null;publ
9、icXmlReader(stringxmlPath,ListBoxlistbox)this.xmlPath=xmlPath;this.listbox=listbox;publicXmlReader(stringxmlPath,ComboBoxcombobox)bobox=combobox;this.xmlPath=xmlPath;#endregion#regionXmlReader資源釋放方法publicvoidDispose()this.Dispose(true);GC.SuppressFinalize(this);protectedvirtualvoidDispose(booldispos
10、ing)if(!disposing)return;if(this.xtr!=null)this.xtr.Close();this.xtr=null;if(this.xmlPath!=null)this.xmlPath=null;#endregion#regionXmlReader的屬性publicListBoxlistBoxgetreturnlistbox;setlistbox=value;publicComboBoxcomboBoxgetreturncombobox;setcombobox=value;publicstringXmlPathgetreturnxmlPath;setxmlPat
11、h=value;#endregion#region遍歷xml文件publicvoidEachXmlToListBox()listbox.Items.Clear();xtr=newXmlTextReader(xmlPath);trywhile(xtr.Read()if(xtr.NodeType!=XmlNodeType.XmlDeclaration)listbox.Items.Add(xtr.Value);catch(XmlExceptionxe)thrownewXmlException(ERRMSG+xe.Message);finallyif(xtr!=null)xtr.Close();pub
12、licvoidEachXmlToComboBox()combobox.Items.Clear();xtr=newXmlTextReader(xmlPath);trywhile(xtr.Read()if(xtr.NodeType!=XmlNodeType.XmlDeclaration)combobox.Items.Add(xtr.Value);catch(XmlExceptionxe)thrownewXmlException(ERRMSG+xe.Message);finallyif(xtr!=null)xtr.Close();#endregion#region讀取XML文件publicvoidR
13、eadXml()stringattAndEle=string.Empty;listbox.Items.Clear();this.xtr=newXmlTextReader(this.xmlPath);/通過路徑,讀取XML文件trywhile(xtr.Read()/按照流方式一個節(jié)點一個節(jié)點的被動讀取。ifthenextnodewasreadsuccessfully,returntrue/publicenumXmlNodeType為枚舉類型,NoneElementAttributeTextCDATAEntityReferenceEntityProcessingInstructionComment
14、DocumentDocumentTypeDocumentFragmentNotationWhitespaceSignificantWhitespaceEndElementEndEntityXmlDeclarationif(xtr.NodeType=XmlNodeType.XmlDeclaration)/XmlDeclaration為XML聲明(例如,)listbox.Items.Add(string.Format(,xtr.Name,xtr.Value);/name為節(jié)點名;value為節(jié)點內(nèi)容elseif(xtr.NodeType=XmlNodeType.Element)/Element元素
15、(例如,)attAndEle=string.Format(;listbox.Items.Add(attAndEle);elseif(xtr.NodeType=XmlNodeType.EndElement)/Anendelementtag(forexample,).listbox.Items.Add(string.Format(,xtr.Name);elseif(xtr.NodeType=XmlNodeType.Text)/Thetextcontentofanode.listbox.Items.Add(xtr.Value);catch(XmlExceptionxmlExp)thrownewXmlException(ERRMSG+this.xmlPath+xmlExp.ToString();finallyif(this.xtr!=null)this.xtr.Close();#endregion#region讀取XML所有文本節(jié)點值publicvoidRe
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 【正版授權(quán)】 IEC TS 63346-1-1:2024 EN Low-voltage auxiliary power systems - Part 1-1: Terminology
- 【正版授權(quán)】 IEC 62386-105:2024 EN-FR Digital addressable lighting interface - Part 105: Particular requirements for control gear and control devices - Firmware transfer
- 【正版授權(quán)】 ISO/IEC TR 19583-24:2025 EN Information technology - Concepts and usage of metadata - Part 24: 11179-3:2013 Metamodel in RDF
- 2025-2030年中國鋅系常溫磷化液市場運營現(xiàn)狀與發(fā)展前景分析報告
- 2025-2030年中國釩鐵行業(yè)市場經(jīng)營狀況及投資戰(zhàn)略研究報告
- 2025江西省安全員B證(項目經(jīng)理)考試題庫
- 2025-2030年中國軟體家具市場運行態(tài)勢及發(fā)展趨勢分析報告
- 2025-2030年中國貝復(fù)舒行業(yè)前景展望及未來投資規(guī)劃研究報告
- 2025-2030年中國蛋品加工市場運營狀況及發(fā)展趨勢分析報告
- 2025-2030年中國管道管產(chǎn)業(yè)前景趨勢及投資戰(zhàn)略研究報告
- 課件-DeepSeek從入門到精通
- 17J008擋土墻(重力式、衡重式、懸臂式)圖示圖集
- 藥品類體外診斷試劑專項培訓(xùn)課件
- 自動化生產(chǎn)線運行與維護完整版課件(全)
- 統(tǒng)計過程控制SPC培訓(xùn)資料
- 回字格+米字格練字模版(A4最大利用率)
- 食品經(jīng)營操作流程圖
- 小學(xué)生必背古詩詞80首硬筆書法字帖
- 中風(fēng)(腦梗死恢復(fù)期)中醫(yī)護理方案(課堂PPT)
- X52K銑床參數(shù)
- 橡膠履帶力學(xué)分析及優(yōu)化設(shè)計
評論
0/150
提交評論