C#實(shí)現(xiàn)提高xml讀寫速度的方法
更新時(shí)間:2014年11月03日 09:25:52 投稿:shichen2014
這篇文章主要介紹了C#實(shí)現(xiàn)提高xml讀寫速度的方法,并且針對(duì)各類文件的讀寫做了較為細(xì)致的分析,非常實(shí)用,需要的朋友可以參考下
本文實(shí)例講述了C#實(shí)現(xiàn)提高xml讀寫速度的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
復(fù)制代碼 代碼如下:
dim domxmldocument as system.xml.xmldocument
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
'窗體加載事件
private sub testxml_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'讀xml過程測(cè)試通過
dim domxmldocument as system.xml.xmldocument
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
dim reader as system.xml.xmlreader = nothing
try
reader = new xml.xmltextreader(xmlfile)
'reader.
while reader.read
me.lboxxml.items.add(reader.name + reader.value)
end while
catch ex as exception
msgbox(ex.message)
finally
if not (reader is nothing) then
reader.close()
end if
end try
end sub
'載入xml事件
private sub btnxmlload_click(byval sender as system.object, byval e as system.eventargs) handles btnxmlload.click
'me.lboxxml.items.clear()
''讀xml過程測(cè)試通過
'dim reader as system.xml.xmlreader = nothing
'try
' reader = new xml.xmltextreader(xmlfile)
' while reader.read
' me.lboxxml.items.add(reader.name + ":" + reader.value)
' end while
'catch ex as exception
' msgbox(ex.message)
'finally
' if not (reader is nothing) then
' reader.close()
' end if
'end try
dim ds as new dataset
try
'如果直接使用ds做datasource則不會(huì)展開datagrid,用dv則能直接顯示正確。
ds.readxml(xmlfile)
dim tb as datatable
dim dv as dataview
tb = ds.tables(0)
dv = new dataview(tb)
datagrid1.datasource = dv
'datagrid1.datamember = "testxmlmember"
'datagrid1.datamember = "employeefname"
'dim dxd as new xmldatadocument
catch ex as exception
msgbox(ex.message.tostring)
end try
end sub
'保存新建xml內(nèi)容事件
private sub btnsavenew_click(byval sender as system.object, byval e as system.eventargs) handles btnsavenew.click
dim mytw as new xmltextwriter(tmppath + "\testxmlwrite.xml", nothing)
mytw.writestartdocument()
mytw.formatting = formatting.indented
mytw.writestartelement("team")
mytw.writestartelement("player")
mytw.writeattributestring("name", "george zip")
mytw.writeattributestring("position", "qb")
mytw.writeelementstring("nickname", "zippy")
mytw.writeelementstring("jerseynumber", xmlconvert.tostring(7))
mytw.writeendelement()
mytw.writeendelement()
mytw.writeenddocument()
mytw.close()
end sub
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
'窗體加載事件
private sub testxml_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
'讀xml過程測(cè)試通過
dim domxmldocument as system.xml.xmldocument
dim tmppath as string = apptempfilepath
dim xmlfile as string = tmppath + "\testxml.xml"
dim reader as system.xml.xmlreader = nothing
try
reader = new xml.xmltextreader(xmlfile)
'reader.
while reader.read
me.lboxxml.items.add(reader.name + reader.value)
end while
catch ex as exception
msgbox(ex.message)
finally
if not (reader is nothing) then
reader.close()
end if
end try
end sub
'載入xml事件
private sub btnxmlload_click(byval sender as system.object, byval e as system.eventargs) handles btnxmlload.click
'me.lboxxml.items.clear()
''讀xml過程測(cè)試通過
'dim reader as system.xml.xmlreader = nothing
'try
' reader = new xml.xmltextreader(xmlfile)
' while reader.read
' me.lboxxml.items.add(reader.name + ":" + reader.value)
' end while
'catch ex as exception
' msgbox(ex.message)
'finally
' if not (reader is nothing) then
' reader.close()
' end if
'end try
dim ds as new dataset
try
'如果直接使用ds做datasource則不會(huì)展開datagrid,用dv則能直接顯示正確。
ds.readxml(xmlfile)
dim tb as datatable
dim dv as dataview
tb = ds.tables(0)
dv = new dataview(tb)
datagrid1.datasource = dv
'datagrid1.datamember = "testxmlmember"
'datagrid1.datamember = "employeefname"
'dim dxd as new xmldatadocument
catch ex as exception
msgbox(ex.message.tostring)
end try
end sub
'保存新建xml內(nèi)容事件
private sub btnsavenew_click(byval sender as system.object, byval e as system.eventargs) handles btnsavenew.click
dim mytw as new xmltextwriter(tmppath + "\testxmlwrite.xml", nothing)
mytw.writestartdocument()
mytw.formatting = formatting.indented
mytw.writestartelement("team")
mytw.writestartelement("player")
mytw.writeattributestring("name", "george zip")
mytw.writeattributestring("position", "qb")
mytw.writeelementstring("nickname", "zippy")
mytw.writeelementstring("jerseynumber", xmlconvert.tostring(7))
mytw.writeendelement()
mytw.writeendelement()
mytw.writeenddocument()
mytw.close()
end sub
文件很大的情況下,可以考慮手動(dòng)實(shí)現(xiàn)數(shù)據(jù)更新適配器,比如手動(dòng)實(shí)現(xiàn)一個(gè)xml節(jié)點(diǎn)搜索/更新,這樣就不用重寫整個(gè)xml。
如果程序的i/o不是主要問題,還是用實(shí)體類整個(gè)的寫入更新吧,畢竟數(shù)據(jù)的完整性是第一位的。
如是文章類的,對(duì)該目錄建一個(gè)xml索引文件來存放文章的編號(hào),url等,用xml的attribute作為標(biāo)記不同字段,內(nèi)容頁面可以用另外的html或xml頁面存放,用linq to xml操作數(shù)據(jù),效率不是很差,個(gè)人觀點(diǎn)。當(dāng)搜索時(shí)候只要查詢指定文件名xml或文件類型就可以了。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
winform實(shí)現(xiàn)關(guān)閉按鈕失效的兩種方法
這篇文章主要介紹了winform實(shí)現(xiàn)關(guān)閉按鈕失效的兩種方法,實(shí)例分析了WinForm實(shí)現(xiàn)關(guān)閉按鈕失效的原理與所涉及的相關(guān)技巧,需要的朋友可以參考下2015-09-09C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)求一組數(shù)據(jù)眾數(shù)的方法,這里以浮點(diǎn)型數(shù)組為例分析了C#求眾數(shù)的算法原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放
在WPF里用MediaElement控件,實(shí)現(xiàn)一個(gè)循環(huán)播放單一視頻的程序,同時(shí)可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實(shí)現(xiàn)視頻循環(huán)播放,需要的朋友參考下吧2018-04-04ListView用法中與滾動(dòng)相關(guān)的需求實(shí)現(xiàn)
這篇文章主要介紹了ListView用法中與滾動(dòng)相關(guān)的需求實(shí)現(xiàn),獲取并設(shè)置ListView的滾動(dòng)位置,以及獲取滾動(dòng)位置處的項(xiàng)目,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06