C#實現提高xml讀寫速度的方法
更新時間:2014年11月03日 09:25:52 投稿:shichen2014
這篇文章主要介紹了C#實現提高xml讀寫速度的方法,并且針對各類文件的讀寫做了較為細致的分析,非常實用,需要的朋友可以參考下
本文實例講述了C#實現提高xml讀寫速度的方法。分享給大家供大家參考。具體實現方法如下:
復制代碼 代碼如下:
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過程測試通過
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過程測試通過
'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則不會展開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內容事件
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過程測試通過
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過程測試通過
'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則不會展開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內容事件
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
文件很大的情況下,可以考慮手動實現數據更新適配器,比如手動實現一個xml節(jié)點搜索/更新,這樣就不用重寫整個xml。
如果程序的i/o不是主要問題,還是用實體類整個的寫入更新吧,畢竟數據的完整性是第一位的。
如是文章類的,對該目錄建一個xml索引文件來存放文章的編號,url等,用xml的attribute作為標記不同字段,內容頁面可以用另外的html或xml頁面存放,用linq to xml操作數據,效率不是很差,個人觀點。當搜索時候只要查詢指定文件名xml或文件類型就可以了。
希望本文所述對大家的C#程序設計有所幫助。
相關文章
C# 使用WPF 用MediaElement控件實現視頻循環(huán)播放
在WPF里用MediaElement控件,實現一個循環(huán)播放單一視頻的程序,同時可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實現視頻循環(huán)播放,需要的朋友參考下吧2018-04-04