C#實(shí)現(xiàn)提高xml讀寫速度的方法
本文實(shí)例講述了C#實(shí)現(xiàn)提高xml讀寫速度的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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過(guò)程測(cè)試通過(guò)
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過(guò)程測(cè)試通過(guò)
'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ì)展開(kāi)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不是主要問(wèn)題,還是用實(shí)體類整個(gè)的寫入更新吧,畢竟數(shù)據(jù)的完整性是第一位的。
如是文章類的,對(duì)該目錄建一個(gè)xml索引文件來(lái)存放文章的編號(hào),url等,用xml的attribute作為標(biāo)記不同字段,內(nèi)容頁(yè)面可以用另外的html或xml頁(yè)面存放,用linq to xml操作數(shù)據(jù),效率不是很差,個(gè)人觀點(diǎn)。當(dāng)搜索時(shí)候只要查詢指定文件名xml或文件類型就可以了。
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入到SQL server數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了在C#中如何實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)入到SQL server數(shù)據(jù)庫(kù)中,文中的示例代碼簡(jiǎn)潔易懂,希望對(duì)大家有一定的幫助2024-03-03
C#緩存之SqlCacheDependency用法實(shí)例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計(jì)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼
利用thrift實(shí)現(xiàn)js與C#通訊的實(shí)例代碼,需要的朋友可以參考一下2013-04-04
C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法
這篇文章主要介紹了C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法,有需要的朋友可以參考一下2014-01-01
C#基于Twain協(xié)議調(diào)用掃描儀,設(shè)置多圖像輸出模式(Multi image output)
這篇文章主要介紹了C#基于Twain協(xié)議調(diào)用掃描儀,設(shè)置多圖像輸出模式(Multi image output)的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-01-01
C# Winform使用log4net進(jìn)行日志記錄
Log4Net是從Java的log4j移植過(guò)來(lái)的,功能也與log4j類似,可以把日志信息輸出到文件、數(shù)據(jù)庫(kù)等不同的介質(zhì)或目標(biāo),下面我們就來(lái)學(xué)習(xí)一下如何使用log4net進(jìn)行日志記錄吧2023-11-11

